﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using ElectricDim;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Plumbing;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYLightingEquipmentLay.Forms;
using HYRElectric.Equipment;
using HYRElectric.WirePlace;
using YJKRGeometry;
using LightSmokeTemperatureCameraAdjust;

namespace HYLightingEquipmentLay
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdRoomLightSetting : HYNoTranExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public override void FilterProcess()
		{
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			if (CmdRoomLightSetting.hWndRevit == null)
			{
				CmdRoomLightSetting.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			FormIndoorAutomaticLayout formIndoorAutomaticLayout = new FormIndoorAutomaticLayout(base.Doc);
			DialogResult dr = formIndoorAutomaticLayout.ShowDialog(CmdRoomLightSetting.hWndRevit);
			int wMsg = 7;
			CmdRoomLightSetting.SendMessage(CmdRoomLightSetting.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			return this.Excute(dr, formIndoorAutomaticLayout.SelectionMethod);
		}

		private Result Excute(DialogResult dr, int selectionMethod)
		{
			if (dr == DialogResult.OK)
			{
				ProgressBarForm progressBarForm = new ProgressBarForm(6, ProgressBarStyle.Blocks, true);
				progressBarForm.Text = "户内自动布置";
				progressBarForm.ShowInTaskbar = false;
				try
				{
					List<RoomLightEquipment> roomLightEquipments = new List<RoomLightEquipment>();
					if (selectionMethod == 0)
					{
						List<Room> roomList = this.GetRoomList();
						if (roomList == null || roomList.Count == 0)
						{
							YJKMessageBox.Show("当前层没有房间！");
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (!this.JudgeRoomSplitInfo(roomList))
						{
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (!this.JudgeElectricalEquipment())
						{
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (!this.JudgeRoomNameExist(roomList))
						{
							YJKMessageBox.Show("未找到房间名称。");
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						progressBarForm.Show(CmdRoomLightSetting.hWndRevit);
						progressBarForm.Text = "获取房间信息...";
						progressBarForm.RefreshProgressBar();
						this.listCurves = this.GetRoomCurves(roomList);
						this.listFilterRooms = this.GetRoomAdjoinRooms(roomList);
						progressBarForm.Text = "房间信息处理...";
						progressBarForm.RefreshProgressBar();
						List<RoomLightEquipment> roomLightEquipmentInfo = this.GetRoomLightEquipmentInfo(roomList);
						roomLightEquipments = this.FilterRoomLightEquipmentOfRoom(roomList, roomLightEquipmentInfo);
					}
					else
					{
						List<Element> list = new List<Element>();
						do
						{
							PickedBox pickedBox = base.UiDoc.Selection.PickBox((Autodesk.Revit.UI.Selection.PickBoxStyle)0, "请框选房间...");
							list = RevitAPIUIOperate.GetAllElement(base.Doc, pickedBox, false);
						}
						while (list.Count == 0);
						List<Room> roomList2 = this.GetRoomList();
						List<Room> list2 = this.RoomFilter2(list, roomList2);
						if (list2 == null || list2.Count == 0)
						{
							YJKMessageBox.Show("没有框选到房间！");
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (!this.JudgeRoomSplitInfo(list2))
						{
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (!this.JudgeElectricalEquipment(list2))
						{
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (!this.JudgeRoomNameExist(list2))
						{
							YJKMessageBox.Show("未找到房间名称。");
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						progressBarForm.Show(CmdRoomLightSetting.hWndRevit);
						progressBarForm.Text = "获取房间信息...";
						progressBarForm.RefreshProgressBar();
						this.listCurves = this.GetRoomCurves(list2);
						this.listFilterRooms = this.GetRoomAdjoinRooms(list2);
						progressBarForm.Text = "房间信息处理...";
						progressBarForm.RefreshProgressBar();
						List<RoomLightEquipment> roomLightEquipmentInfo2 = this.GetRoomLightEquipmentInfo(list2);
						roomLightEquipments = this.FilterRoomLightEquipmentOfRoom(list2, roomLightEquipmentInfo2);
					}
					progressBarForm.Text = "布置灯具...";
					progressBarForm.RefreshProgressBar();
					List<RoomLightEquipment> roomLightEquipments2 = this.LayLampsAndLanterns(roomLightEquipments);
					progressBarForm.Text = "布置开关...";
					progressBarForm.RefreshProgressBar();
					int num;
					List<RoomLightEquipment> roomLightEquipments3 = this.CalculationSwitchsPoint(roomLightEquipments2, out num);
					List<List<RoomLightEquipment>> list3 = new List<List<RoomLightEquipment>>();
					progressBarForm.Text = "设备连线...";
					progressBarForm.RefreshProgressBar();
					if (this.JudgeElectricalEquipment(roomLightEquipments3, list3))
					{
                        //new WireParamNon_modeForm(base.Doc, false).FormInfo();
                        //this.LampsAndSwitchsConnectLine(roomLightEquipments3);
                        //foreach (List<RoomLightEquipment> roomLightEquipments4 in list3)
                        //{
                        //    this.LampsConnectLine(roomLightEquipments4);
                        //}
                        //EquipmentConnectWires.CreatWireByElementList(this.listConnectWires, base.Doc);
					}
					progressBarForm.Text = "提交中...";
					progressBarForm.RefreshProgressBar();
					if (num > 0)
					{
						StringBuilder stringBuilder = new StringBuilder();
						stringBuilder.AppendLine("有" + num.ToString() + "个房间布置开关失败！可能原因如下:");
						stringBuilder.AppendLine("1.房间没有归属的门和分割线；");
						stringBuilder.AppendLine("2.房间有门但族不对。");
						YJKMessageBox.Show(stringBuilder.ToString());
					}
				}
				catch (Exception)
				{
					progressBarForm.Close();
				}
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Dictionary<Room, List<Curve>> GetRoomCurves(List<Room> listRooms)
		{
			Dictionary<Room, List<Curve>> dictionary = new Dictionary<Room, List<Curve>>();
			foreach (Room room in listRooms)
			{
				List<Curve> spaceOrRoomBoundaryCurve = YJKRevitTools.GetSpaceOrRoomBoundaryCurve(room);
				dictionary.Add(room, spaceOrRoomBoundaryCurve);
			}
			return dictionary;
		}

		private List<Room> GetRoomList()
		{
			IList<Element> list = new List<Element>();
			list = new HYSystemElementFilter(base.Doc).GetSpecElementSet((Autodesk.Revit.DB.BuiltInCategory)(-2000160));
			double[] viewRange = RevitAPIUIOperate.GetViewRange2(base.Doc.ActiveView, false);
			IEnumerable<Document> linkDocuments = base.Doc.GetLinkDocuments();
			if (linkDocuments != null)
			{
				foreach (Autodesk.Revit.DB.Document document in linkDocuments)
				{
					if (document != null)
					{
						foreach (Element element in ((IEnumerable<Element>)new HYSystemElementFilter(document).GetElementsByBICategory(BuiltInCategory.OST_Rooms).ToList<Element>()))
						{
							LocationPoint locationPoint = element.Location as LocationPoint;
							if (locationPoint != null && locationPoint.Point.Z >= viewRange[0] && locationPoint.Point.Z < viewRange[1])
							{
								list.Add(element);
							}
						}
					}
				}
			}
			return this.RoomFilter(list.ToList<Element>());
		}

		private bool JudgeRoomSplitInfo(List<Room> listRoom)
		{
			List<Room> list = new List<Room>();
			foreach (Room room in listRoom)
			{
				if (room.GetParameter("户型名称") != null && room.GetParameter("户型编号") != null && room.GetParameter("单元") != null && room.GetParameter("楼号") != null && room.GetParameter("户型名称").AsString() != null && room.GetParameter("户型编号").AsString() != null && room.GetParameter("单元").AsString() != null && room.GetParameter("楼号").AsString() != null && room.GetParameter("户型名称").AsString() != "" && room.GetParameter("户型编号").AsString() != "" && room.GetParameter("单元").AsString() != "" && room.GetParameter("楼号").AsString() != "")
				{
					list.Add(room);
				}
			}
			if (list == null || list.Count == 0)
			{
				YJKMessageBox.Show("未找到户型分割信息。");
				return false;
			}
			listRoom = new List<Room>();
			listRoom.AddRange(list);
			return true;
		}

		private bool JudgeElectricalEquipment()
		{
			IList<Element> specElementSet = new HYSystemElementFilter(base.Doc).GetSpecElementSet((Autodesk.Revit.DB.BuiltInCategory)(-2001040));
			double[] viewRange = RevitAPIUIOperate.GetViewRange2(base.Doc.ActiveView, false);
			bool flag = false;
			IEnumerable<Document> linkDocuments = base.Doc.GetLinkDocuments();
			if (linkDocuments != null)
			{
				foreach (Autodesk.Revit.DB.Document document in linkDocuments)
				{
					if (document != null)
					{
						foreach (Element element in ((IEnumerable<Element>)new HYSystemElementFilter(document).GetElementsByBICategory(BuiltInCategory.OST_ElectricalEquipment).ToList<Element>()))
						{
							if (element is FamilyInstance)
							{
								LocationPoint locationPoint = element.Location as LocationPoint;
								if (locationPoint != null && locationPoint.Point.Z >= viewRange[0] && locationPoint.Point.Z < viewRange[1])
								{
									specElementSet.Add(element);
								}
							}
						}
					}
				}
			}
			foreach (Element element2 in specElementSet)
			{
				FamilyInstance familyInstance = element2 as FamilyInstance;
				FamilySymbol symbol = familyInstance.Symbol;
				if (symbol.GetFamilyNameBySymbol().Contains("照明配电箱") || symbol.GetFamilyNameBySymbol().Contains("家居配电箱") || symbol.GetFamilyNameBySymbol().Contains("住户配电箱") || symbol.GetFamilyNameBySymbol().Contains("户内配电箱") || symbol.GetFamilyNameBySymbol().Contains("住宅家居配电箱") || (familyInstance.GetParameter("名称") != null && familyInstance.GetParameter("名称").AsString().Contains("家居配电箱")) || (symbol.GetParameter("名称") != null && symbol.GetParameter("名称").AsString().Contains("家居配电箱")))
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				YJKMessageBox.Show("未找到住户配电箱。");
				return false;
			}
			return true;
		}

		private bool JudgeElectricalEquipment(List<Room> listRoom)
		{
			foreach (Room room in listRoom)
			{
				BoundingBoxXYZ boundingBoxXYZ = room.get_BoundingBox(base.Doc.ActiveView);
				foreach (Element element in this.GetElementList(boundingBoxXYZ.Max, boundingBoxXYZ.Min, BuiltInCategory.OST_ElectricalEquipment))
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (familyInstance != null)
					{
						FamilySymbol symbol = familyInstance.Symbol;
						if (symbol.GetFamilyNameBySymbol().Contains("照明配电箱") || symbol.GetFamilyNameBySymbol().Contains("家居配电箱") || symbol.GetFamilyNameBySymbol().Contains("住户配电箱") || symbol.GetFamilyNameBySymbol().Contains("户内配电箱") || symbol.GetFamilyNameBySymbol().Contains("住宅家居配电箱") || (familyInstance.GetParameter("名称") != null && familyInstance.GetParameter("名称").AsString().Contains("家居配电箱")) || (symbol.GetParameter("名称") != null && symbol.GetParameter("名称").AsString().Contains("家居配电箱")))
						{
							return true;
						}
					}
				}
			}
			YJKMessageBox.Show("未找到住户配电箱。");
			return false;
		}

		private bool JudgeRoomNameExist(List<Room> listRoom)
		{
			List<Room> list = new List<Room>();
			foreach (Room room in listRoom)
			{
				if (room.GetParameter("名称") != null && room.GetParameter("名称").AsString() != null && room.GetParameter("名称").AsString() != "")
				{
					list.Add(room);
				}
			}
			if (list == null || list.Count == 0)
			{
				return false;
			}
			listRoom = new List<Room>();
			listRoom.AddRange(list);
			return true;
		}

		private bool JudgeElectricalEquipment(List<RoomLightEquipment> roomLightEquipments, List<List<RoomLightEquipment>> roomEquipments)
		{
			foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
			{
				if (roomLightEquipment.Room.GetParameter("户型名称") != null && roomLightEquipment.Room.GetParameter("户型编号") != null && roomLightEquipment.Room.GetParameter("单元") != null && roomLightEquipment.Room.GetParameter("楼号") != null && roomLightEquipment.Room.GetParameter("户型名称").AsString() != null && roomLightEquipment.Room.GetParameter("户型编号").AsString() != null && roomLightEquipment.Room.GetParameter("单元").AsString() != null && roomLightEquipment.Room.GetParameter("楼号").AsString() != null && roomLightEquipment.Room.GetParameter("户型名称").AsString() != "" && roomLightEquipment.Room.GetParameter("户型编号").AsString() != "" && roomLightEquipment.Room.GetParameter("单元").AsString() != "" && roomLightEquipment.Room.GetParameter("楼号").AsString() != "")
				{
					bool flag = true;
					foreach (List<RoomLightEquipment> list in roomEquipments)
					{
						if (roomLightEquipment.Room.GetParameter("户型名称").AsString() == list[0].Room.GetParameter("户型名称").AsString() && roomLightEquipment.Room.GetParameter("户型编号").AsString() == list[0].Room.GetParameter("户型编号").AsString() && roomLightEquipment.Room.GetParameter("单元").AsString() == list[0].Room.GetParameter("单元").AsString() && roomLightEquipment.Room.GetParameter("楼号").AsString() == list[0].Room.GetParameter("楼号").AsString())
						{
							flag = false;
							list.Add(roomLightEquipment);
							break;
						}
					}
					if (flag)
					{
						roomEquipments.Add(new List<RoomLightEquipment>
						{
							roomLightEquipment
						});
					}
				}
			}
			return true;
		}

		private int JudgeRoomType(Element element)
		{
			List<string> roomName = this.GetRoomName(0);
			List<string> roomName2 = this.GetRoomName(1);
			string text = "";
			if (element.GetParameter("名称") != null)
			{
				text = element.GetParameter("名称").AsString();
			}
			int result = 0;
			foreach (string value in roomName)
			{
				if (text.Contains(value))
				{
					result = 1;
				}
			}
			foreach (string value2 in roomName2)
			{
				if (text.Contains(value2))
				{
					result = 2;
				}
			}
			return result;
		}

		private int JudgeRoomOfSwitchLay(Element element)
		{
			List<string> roomName = this.GetRoomName(2);
			List<string> roomName2 = this.GetRoomName(3);
			List<string> roomName3 = this.GetRoomName(4);
			string text = "";
			if (element.GetParameter("名称") != null)
			{
				text = element.GetParameter("名称").AsString();
			}
			int result = 0;
			foreach (string value in roomName)
			{
				if (text.Contains(value))
				{
					result = 1;
				}
			}
			foreach (string value2 in roomName2)
			{
				if (text.Contains(value2))
				{
					result = 2;
				}
			}
			foreach (string value3 in roomName3)
			{
				if (text.Contains(value3))
				{
					result = 3;
				}
			}
			return result;
		}

		private List<RoomLightEquipment> LayLampsAndLanterns(List<RoomLightEquipment> roomLightEquipments)
		{
			List<DbFamilyObject> familyObjects = new FamilyLibDataAssist(base.Doc, "Lamp").LoadFamilyDatas(null);
			List<BuiltInCategory> list = new List<BuiltInCategory>();
			list.Add((Autodesk.Revit.DB.BuiltInCategory)(-2001120));
			List<Element> list2 = new List<Element>();
			TransactionGroup transactionGroup = new TransactionGroup(base.Doc, "TransactionGroup");
			try
			{
				transactionGroup.Start();
				Transaction transaction = null;
				try
				{
					List<XYZ> list3 = new List<XYZ>();
					foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
					{
						transaction = new Transaction(base.Doc, "RectLayoutEx");
						transaction.Start();
						Room room = roomLightEquipment.Room;
						if (room.GetBoundarySegments(new SpatialElementBoundaryOptions()).Count == 0)
						{
							transaction.RollBack();
						}
						else
						{
							XYZ xyz = YJKRevitTools.Centroid(room);
							if (xyz == null)
							{
								xyz = (room.Location as LocationPoint).Point;
							}
							if (xyz != null && !list3.Contains(xyz))
							{
								list3.Add(xyz);
								double angle = 0.0;
								DbFamilyObject dbFamilyObject = null;
								int num = this.JudgeRoomType(room);
								if (num == 0)
								{
									transaction.RollBack();
								}
								else
								{
									FamilySymbol familySymbol = this.GetFamilySymbol(num, familyObjects, ref dbFamilyObject);
									EquipmentLayout equipmentLayout = new CenterRoomLayout(base.UiDoc, base.Doc, dbFamilyObject, familySymbol, list);
									FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(base.Doc, xyz, familySymbol, base.Doc.ActiveView.GenLevel, angle);
									if (familyInstance == null)
									{
										throw new Exception("布置设备出错。");
									}
									equipmentLayout.SetParametersValue(familyInstance, dbFamilyObject.Size);
									ElementParameter.SetLevel(familyInstance, 0.0);
									transaction.Commit();
									if (familyInstance.IsValidObject)
									{
										roomLightEquipment.Lamps = familyInstance;
										list2.Add(familyInstance);
									}
								}
							}
							else
							{
								transaction.RollBack();
							}
						}
					}
				}
				catch (Exception)
				{
					if (transaction != null && (int)transaction.GetStatus() != 3)
					{
						transaction.RollBack();
					}
				}
				transactionGroup.Assimilate();
			}
			catch (Exception)
			{
				transactionGroup.RollBack();
				return null;
			}
			LampsMounting.LampsSuctionTop(base.Doc, list2, false);
			return roomLightEquipments;
		}

		private List<RoomLightEquipment> LaySwitchs(List<RoomLightEquipment> roomLightEquipments)
		{
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			List<DbFamilyObject> familyObjects = new FamilyLibDataAssist(base.Doc, "Switch").LoadFamilyDatas(null);
			List<BuiltInCategory> list2 = new List<BuiltInCategory>();
			list2.Add((Autodesk.Revit.DB.BuiltInCategory)(-2008087));
			try
			{
				List<Wall> alldocWaLLs = this.getALLDocWaLLs();
				foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
				{
					if (roomLightEquipment.Point != null && roomLightEquipment.Ascription != 1 && roomLightEquipment.Switchs.Count == 0)
					{
						DbFamilyObject dbFamilyObject = null;
						FamilySymbol familySymbol = this.GetFamilySymbol2(roomLightEquipment.SwitchType, familyObjects, ref dbFamilyObject);
						WallCrossLayout wallCrossLayout = new WallCrossLayout(base.UiDoc, base.Doc, dbFamilyObject, familySymbol, list2);
						XYZ xyz = new XYZ();
						XYZ xyz2 = new XYZ();
						Room room = roomLightEquipment.Room;
						int flag;
						if (roomLightEquipment.InOrOutLaySwitch == 0)
						{
							int num = this.JudgeRoomOfSwitchLay(room);
							flag = 1;
							if (num == 2)
							{
								flag = -1;
							}
						}
						else
						{
							flag = roomLightEquipment.InOrOutLaySwitch;
						}
						if (this.CalStartPointEndPoint(roomLightEquipment, ref xyz, ref xyz2, flag))
						{
							YJKCurve hycurve = new YJKCurve(Line.CreateBound(xyz, xyz2));
							List<XYZ> list3 = new List<XYZ>();
							List<double> list4 = new List<double>();
							foreach (Wall wall in alldocWaLLs)
							{
								Curve curve = null;
								Curve curve2 = null;
								wallCrossLayout.GetWallSide(ref curve, ref curve2, wall);
								if (!(curve == null) && !(curve2 == null))
								{
									XYZ xyz3 = hycurve.IntersectionPoint(curve);
									XYZ xyz4 = hycurve.IntersectionPoint(curve2);
									if (xyz3 != null && xyz4 != null)
									{
										Curve curve3 = (wall.Location as LocationCurve).Curve;
										XYZ xyz5 = (xyz.DistanceTo(xyz3) < xyz.DistanceTo(xyz4)) ? xyz3 : xyz4;
										IntersectionResult intersectionResult = curve3.Project(xyz5);
										if (intersectionResult != null)
										{
											XYZ xyzpoint = intersectionResult.XYZPoint;
											XYZ xyz6 = xyz5 - xyzpoint;
											double num2 = XYZ.BasisY.AngleOnPlaneTo(xyz6, XYZ.BasisZ);
                                            //xyz5 - 2.0 * xyz6;
											double num3 = num2 + Math.PI;
											if (num3 > Math.PI*2)
											{
												num3 -= Math.PI*2;
											}
											list3.Add(xyz5);
											list4.Add(num2);
										}
									}
								}
							}
							for (int i = 0; i < list3.Count - 1; i++)
							{
								for (int j = i + 1; j < list3.Count; j++)
								{
									if (xyz.DistanceTo(list3[i]) > xyz.DistanceTo(list3[j]))
									{
										XYZ value = list3[i];
										list3[i] = list3[j];
										list3[j] = value;
										double value2 = list4[i];
										list4[i] = list4[j];
										list4[j] = value2;
									}
								}
							}
							for (int k = 0; k < list3.Count; k++)
							{
								XYZ xyz7 = list3[k];
								double angle = list4[k] * 180.0 / Math.PI;
								FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(base.Doc, xyz7, familySymbol, base.Doc.ActiveView.GenLevel, angle);
								if (familyInstance == null)
								{
									throw new Exception("布置设备出错。");
								}
								wallCrossLayout.SetParametersValue(familyInstance, dbFamilyObject.Size);
								ElementParameter.SetLevel(familyInstance, 1.3);
								roomLightEquipment.Switchs.Add(familyInstance);
							}
							list.Add(roomLightEquipment);
						}
					}
				}
			}
			catch (Exception)
			{
			}
			return list;
		}

		private bool CalStartPointEndPoint(RoomLightEquipment roomLightEquipment, ref XYZ startPoint, ref XYZ endPoint, int flag)
		{
			XYZ point = roomLightEquipment.Point;
			XYZ point2 = new XYZ(point.X + 0.1, point.Y + 0.1, 0.0);
			XYZ point3 = new XYZ(point.X - 0.1, point.Y - 0.1, 0.0);
			IEnumerable<IList<BoundarySegment>> boundarySegments = roomLightEquipment.Room.GetBoundarySegments(new SpatialElementBoundaryOptions());
			List<Element> list = new List<Element>();
			foreach (IList<BoundarySegment> source in boundarySegments)
			{
				list.AddRange(from segment in source
				select UnifiedModified.GetBoundarySegmentElement(base.Doc, segment));
			}
			Wall wall;
			if (roomLightEquipment.Wall != null)
			{
				wall = roomLightEquipment.Wall;
			}
			else
			{
				List<Element> list2 = this.GetWallList(point2, point3);
				if (list2 == null || list2.Count == 0)
				{
					wall = null;
				}
				else if (list2.Count > 1)
				{
					List<Element> list3 = new List<Element>();
					foreach (Element element in list2)
					{
						bool flag2 = false;
						foreach (Element element2 in list)
						{
							if (element2 != null && element2 is Wall && element2.Id == element.Id)
							{
								flag2 = true;
							}
						}
						if (flag2)
						{
							list3.Add(element);
						}
					}
					list2 = list3;
					if (list2.Count == 0)
					{
						wall = null;
					}
					else
					{
						wall = (list2[0] as Wall);
					}
				}
				else
				{
					wall = (list2[0] as Wall);
				}
			}
			if (wall == null)
			{
				return false;
			}
			double num = wall.WallType.GetParameter("厚度").AsDouble();
			LocationCurve locationCurve = wall.Location as LocationCurve;
			XYZ project = new YJKLine(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1)).GetProject(point);
			XYZ xyz = new XYZ(point.X, point.Y, project.Z);
			XYZ xyz2 = (xyz - project).Normalize();
			startPoint = xyz + xyz2 * 2.0 * num * (double)flag;
			endPoint = xyz - xyz2 * 2.0 * num * (double)flag;
			return true;
		}

		private List<Wall> getALLDocWaLLs()
		{
			List<Wall> list = new List<Wall>();
			List<Document> linkDocs = CmdRoomLightSetting.GetLinkDocs(base.Doc);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(base.Doc, base.Doc.ActiveView.Id);
			filteredElementCollector.WherePasses(new ElementClassFilter(typeof(Wall)));
			foreach (Element element in filteredElementCollector.ToElements())
			{
				if (element != null)
				{
					Wall item = element as Wall;
					list.Add(item);
				}
			}
			double elevation = base.Doc.ActiveView.GenLevel.Elevation;
			foreach (Autodesk.Revit.DB.Document document in linkDocs)
			{
				if (document != null)
				{
					FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(document);
					filteredElementCollector2.WherePasses(new ElementClassFilter(typeof(Wall)));
					foreach (Element element2 in filteredElementCollector2.ToElements())
					{
						if (element2 != null)
						{
							Wall wall = element2 as Wall;
							if (Math.Abs((document.GetElementById(wall.LevelId) as Level).Elevation - elevation) < 0.001)
							{
								list.Add(wall);
							}
						}
					}
				}
			}
			return list;
		}

		private static List<Document> GetLinkDocs(Autodesk.Revit.DB.Document doc)
		{
			List<Document> list = new List<Document>();
			try
			{
				foreach (RevitLinkInstance revitLinkInstance in ((IEnumerable<RevitLinkInstance>)new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance)).Cast<RevitLinkInstance>().ToList<RevitLinkInstance>()))
				{
					list.Add(revitLinkInstance.GetLinkDocument());
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return list;
		}

		private List<RoomLightEquipment> CalculationSwitchsPoint(List<RoomLightEquipment> roomLightEquipments, out int errorNumber)
		{
			Transaction transaction = new Transaction(base.Doc, "布置开关");
			transaction.Start();
			errorNumber = 0;
			List<RoomLightEquipment> result;
			try
			{
				List<RoomLightEquipment> list = new List<RoomLightEquipment>();
				foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
				{
					RoomLightEquipment roomLightEquipment2 = new RoomLightEquipment();
					Room room = roomLightEquipment.Room;
					int num = this.JudgeRoomOfSwitchLay(room);
					if (roomLightEquipment.Doors.Count == 0 && num != 0)
					{
						num = 3;
					}
					switch (num)
					{
					case 0:
						roomLightEquipment2 = null;
						errorNumber--;
						break;
					case 1:
						roomLightEquipment2 = this.LaySwichInRoom(roomLightEquipment);
						break;
					case 2:
						roomLightEquipment2 = this.LaySwichOutRoom(roomLightEquipment);
						break;
					case 3:
						roomLightEquipment2 = this.LaySwitchByLine(roomLightEquipment);
						break;
					}
					if (roomLightEquipment2 != null)
					{
						list.Add(roomLightEquipment2);
					}
					else
					{
						errorNumber++;
					}
				}
				List<RoomLightEquipment> roomlightEquipments = this.LaySwitchs(list);
				List<RoomLightEquipment> roomLightEquipments2 = this.MergeSwitchs(roomlightEquipments);
				this.LaySwitchs(roomLightEquipments2);
				transaction.Commit();
				result = roomLightEquipments;
			}
			catch (Exception)
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				result = null;
			}
			return result;
		}

		private List<RoomLightEquipment> MergeSwitchs(List<RoomLightEquipment> roomlightEquipments)
		{
			foreach (RoomLightEquipment roomLightEquipment in roomlightEquipments)
			{
				foreach (RoomLightEquipment roomLightEquipment2 in roomlightEquipments)
				{
					if (roomLightEquipment.Room.Id != roomLightEquipment2.Room.Id && roomLightEquipment.Switchs.Count != 0 && roomLightEquipment2.Switchs.Count != 0 && roomLightEquipment.Ascription == 0 && roomLightEquipment2.Ascription == 0 && (roomLightEquipment.Switchs[0].Location as LocationPoint).Point.DistanceTo((roomLightEquipment2.Switchs[0].Location as LocationPoint).Point) < FormIndoorAutomaticLayoutSetting.SwitchMerging / 304.8)
					{
						XYZ point = (roomLightEquipment.Switchs[0].Location as LocationPoint).Point;
						XYZ point2 = (roomLightEquipment2.Switchs[0].Location as LocationPoint).Point;
						List<Element> elements = this.GetElements(point, 0.1);
						List<Element> elements2 = this.GetElements(point2, 0.1);
						if (elements != null && elements2 != null && elements.Count != 0 && elements2.Count != 0 && elements[0].Id == elements2[0].Id)
						{
							Curve curve = (elements[0].Location as LocationCurve).Curve;
							YJKLine hyline = new YJKLine(point, point2);
							Line otherLine = Line.CreateBound(curve.GetEndPoint(0), curve.GetEndPoint(1));
							if (hyline.IsParallel(otherLine))
							{
								List<Curve> lstCurves = this.listCurves[roomLightEquipment.Room];
								if (this.PointInPloygon(lstCurves, (roomLightEquipment.Switchs[0].Location as LocationPoint).Point) == 1)
								{
									roomLightEquipment.Ascription = 2;
									roomLightEquipment2.Ascription = 1;
								}
								else
								{
									roomLightEquipment.Ascription = 1;
									roomLightEquipment2.Ascription = 2;
								}
								roomLightEquipment.SwitchType += roomLightEquipment2.SwitchType;
								roomLightEquipment2.SwitchType = roomLightEquipment.SwitchType;
								base.Doc.DeleteElement(roomLightEquipment.Switchs[0]);
								base.Doc.DeleteElement(roomLightEquipment2.Switchs[0]);
								roomLightEquipment.Switchs = new List<Element>();
								roomLightEquipment2.Switchs = new List<Element>();
							}
						}
					}
				}
			}
			return roomlightEquipments;
		}

		private List<RoomLightEquipment> LampsAndSwitchsConnectLine(List<RoomLightEquipment> roomLightEquipments)
		{
			foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
			{
				if (roomLightEquipment.Ascription != 1)
				{
					List<Element> list = new List<Element>();
					if (roomLightEquipment.Lamps != null && roomLightEquipment.Switchs.Count != 0 && roomLightEquipment.Switchs[0].IsValidObject)
					{
						list.Add(roomLightEquipment.Lamps);
						list.Add(roomLightEquipment.Switchs[0]);
						this.listConnectWires.Add(list);
						RoomLightEquipment roomLightEquipment2 = roomLightEquipment;
						int connectNumber = roomLightEquipment2.ConnectNumber;
						roomLightEquipment2.ConnectNumber = connectNumber + 1;
					}
				}
			}
			return roomLightEquipments;
		}

		private bool LampsConnectLine(List<RoomLightEquipment> roomLightEquipments)
		{
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			List<RoomLightEquipment> list2 = new List<RoomLightEquipment>();
			List<RoomLightEquipment> list3 = new List<RoomLightEquipment>();
			List<string> roomName = this.GetRoomName(6);
			List<string> roomName2 = this.GetRoomName(7);
			string text = "";
			foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
			{
				if (roomLightEquipment.Lamps != null)
				{
					if (roomLightEquipment.Room.GetParameter("名称") != null)
					{
						text = roomLightEquipment.Room.GetParameter("名称").AsString();
					}
					bool flag = true;
					foreach (string value in roomName)
					{
						if (text.Contains(value))
						{
							flag = false;
							list.Add(roomLightEquipment);
							break;
						}
					}
					if (flag)
					{
						foreach (string value2 in roomName2)
						{
							if (text.Contains(value2))
							{
								flag = false;
								list3.Add(roomLightEquipment);
								break;
							}
						}
					}
					if (flag)
					{
						list2.Add(roomLightEquipment);
					}
				}
			}
			List<Element> list4 = this.LookForElectricalEquipmentOfRoom(list);
			if (list4 != null && list4.Count != 0)
			{
				List<RoomLightEquipment> equipments = this.CommonAreaLampsConnectLine(list, list4[0]);
				List<List<RoomLightEquipment>> connLamps = new List<List<RoomLightEquipment>>();
				connLamps = this.FirstConnRooms(roomLightEquipments);
				this.SetOtherAreaIsConnTrue(list2, connLamps);
				List<RoomLightEquipment> roomLightEquipments2 = this.OtherAreaConnect(equipments, list2, connLamps, list3);
				this.LastConnAreas(roomLightEquipments2, list3, connLamps);
			}
			return true;
		}

		private void SetOtherAreaIsConnTrue(List<RoomLightEquipment> listOtherArea, List<List<RoomLightEquipment>> connLamps)
		{
			List<string> roomName = this.GetRoomName(6);
			string text = "";
			string text2 = "";
			List<ElementId> list = new List<ElementId>();
			foreach (List<RoomLightEquipment> list2 in connLamps)
			{
				if (list2.Count != 2)
				{
					return;
				}
				if (list2[0].Room.GetParameter("名称") != null)
				{
					text = list2[0].Room.GetParameter("名称").AsString();
				}
				if (list2[1].Room.GetParameter("名称") != null)
				{
					text2 = list2[1].Room.GetParameter("名称").AsString();
				}
				foreach (string value in roomName)
				{
					if (text.Contains(value))
					{
						list.Add(list2[1].Room.Id);
						break;
					}
					if (text2.Contains(value))
					{
						list.Add(list2[0].Room.Id);
						break;
					}
				}
			}
			foreach (RoomLightEquipment roomLightEquipment in listOtherArea)
			{
				if (list.Contains(roomLightEquipment.Room.Id))
				{
					roomLightEquipment.IsConn = true;
				}
			}
		}

		private List<List<RoomLightEquipment>> FirstConnRooms(List<RoomLightEquipment> roomLightEquipments)
		{
			Dictionary<List<string>, List<string>> ascriptionInfo = this.GetAscriptionInfo(9);
			List<string> roomName = this.GetRoomName(7);
			List<List<RoomLightEquipment>> list = new List<List<RoomLightEquipment>>();
			foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
			{
				List<Element> list2 = this.listFilterRooms[roomLightEquipment.Room];
				if (list2 != null && list2.Count != 0)
				{
					List<RoomLightEquipment> list3 = new List<RoomLightEquipment>();
					foreach (RoomLightEquipment roomLightEquipment2 in roomLightEquipments)
					{
						foreach (Element element in list2)
						{
							if (roomLightEquipment2.Room.Id == element.Id)
							{
								list3.Add(roomLightEquipment2);
								break;
							}
						}
					}
					List<RoomLightEquipment> list4 = this.ConnRooms(roomLightEquipment, list3, ascriptionInfo);
					if (list4 != null)
					{
						bool flag = true;
						int i = 0;
						while (i < list.Count)
						{
							if (!(list[i][0].Room.Id == list4[0].Room.Id))
							{
								goto IL_1AD;
							}
							if (list[i][0].Room.GetParameter("名称") != null)
							{
								string text = list[i][0].Room.GetParameter("名称").AsString();
								foreach (string value in roomName)
								{
									if (text.Contains(value))
									{
										flag = false;
										break;
									}
								}
								goto IL_1AD;
							}
							IL_214:
							i++;
							continue;
							IL_1AD:
							if (list[i][1].Room.Id == list4[0].Room.Id && list[i][0].Room.Id == list4[1].Room.Id)
							{
								flag = false;
								break;
							}
							goto IL_214;
						}
						if (flag)
						{
							list.Add(list4);
						}
					}
				}
			}
			foreach (List<RoomLightEquipment> list5 in list)
			{
				if (list5[0].Lamps != null && list5[1].Lamps != null)
				{
					List<Element> list6 = new List<Element>();
					list6.Add(list5[0].Lamps);
					list6.Add(list5[1].Lamps);
					this.listConnectWires.Add(list6);
				}
			}
			return list;
		}

		private List<RoomLightEquipment> ConnRooms(RoomLightEquipment roomLightEquipment, List<RoomLightEquipment> listRooms, Dictionary<List<string>, List<string>> dic)
		{
			if (roomLightEquipment.Room.GetParameter("名称") == null)
			{
				return null;
			}
			string text = roomLightEquipment.Room.GetParameter("名称").AsString();
			bool flag = false;
			bool flag2 = false;
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			foreach (RoomLightEquipment roomLightEquipment2 in listRooms)
			{
				if (roomLightEquipment2.Room.GetParameter("名称") != null)
				{
					flag = false;
					flag2 = false;
					string text2 = roomLightEquipment2.Room.GetParameter("名称").AsString();
					foreach (KeyValuePair<List<string>, List<string>> keyValuePair in dic)
					{
						foreach (string value in keyValuePair.Key)
						{
							if (text.Contains(value))
							{
								flag = true;
								break;
							}
						}
						foreach (string value2 in keyValuePair.Value)
						{
							if (text2.Contains(value2))
							{
								flag2 = true;
								break;
							}
						}
						if (flag && flag2)
						{
							list.Add(roomLightEquipment2);
							break;
						}
						flag = false;
						flag2 = false;
					}
				}
			}
			if (list != null && list.Count == 1)
			{
				return new List<RoomLightEquipment>
				{
					roomLightEquipment,
					list[0]
				};
			}
			if (list != null && list.Count > 1)
			{
				return this.GetFirstConnEquipment(roomLightEquipment, list);
			}
			return null;
		}

		private List<RoomLightEquipment> GetFirstConnEquipment(RoomLightEquipment roomLightEquipment, List<RoomLightEquipment> listRoomLightEquipments)
		{
			RoomLightEquipment roomLightEquipment2 = new RoomLightEquipment();
			double num = 10000.0;
			foreach (RoomLightEquipment roomLightEquipment3 in listRoomLightEquipments)
			{
				double roomDistance = this.GetRoomDistance(roomLightEquipment, roomLightEquipment3);
				if (num > roomDistance)
				{
					num = roomDistance;
					roomLightEquipment2 = roomLightEquipment3;
				}
			}
			if (roomLightEquipment2.Room == null || roomLightEquipment2.Lamps == null)
			{
				return null;
			}
			return new List<RoomLightEquipment>
			{
				roomLightEquipment,
				roomLightEquipment2
			};
		}

		private double GetRoomDistance(RoomLightEquipment roomLightEquipment, RoomLightEquipment otherRoomLightEquipment)
		{
			double num = 10001.0;
			List<Curve> list = this.listCurves[roomLightEquipment.Room];
			if (list.Count == 0)
			{
				return num;
			}
			bool flag = false;
			List<Curve> list2 = GroundStorehouseNozzle.MergeCurve(GroundStorehouseNozzle.SortListCurves(list, out flag));
			List<Curve> list3 = this.listCurves[otherRoomLightEquipment.Room];
			if (list3.Count == 0)
			{
				return num;
			}
			List<Curve> list4 = GroundStorehouseNozzle.MergeCurve(GroundStorehouseNozzle.SortListCurves(list3, out flag));
			foreach (Curve curve in list2)
			{
				YJKLine hyline = new YJKLine(curve.GetEndPoint(0), curve.GetEndPoint(1));
				hyline.MakeUnBound();
				XYZ xyz = (curve.GetEndPoint(0) + curve.GetEndPoint(1)) / 2.0;
				foreach (Curve curve2 in list4)
				{
					if (hyline.IsParallel(curve2 as Line))
					{
						XYZ point = (curve2.GetEndPoint(0) + curve2.GetEndPoint(1)) / 2.0;
						XYZ project = hyline.GetProject(point);
						double num2 = xyz.DistanceTo(project);
						if (num > num2)
						{
							num = num2;
						}
					}
				}
			}
			return num;
		}

		private List<Element> LookForElectricalEquipmentOfRoom(List<RoomLightEquipment> listCommonArea)
		{
			List<Element> list = new List<Element>();
			foreach (RoomLightEquipment roomLightEquipment in listCommonArea)
			{
				BoundingBoxXYZ boundingBoxXYZ = roomLightEquipment.Room.get_BoundingBox(base.Doc.ActiveView);
				foreach (Element element in this.GetElementList(boundingBoxXYZ.Max, boundingBoxXYZ.Min, BuiltInCategory.OST_ElectricalEquipment))
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (familyInstance != null)
					{
						FamilySymbol symbol = familyInstance.Symbol;
						if (symbol.GetFamilyNameBySymbol().Contains("照明配电箱") || symbol.GetFamilyNameBySymbol().Contains("家居配电箱") || symbol.GetFamilyNameBySymbol().Contains("住户配电箱") || symbol.GetFamilyNameBySymbol().Contains("户内配电箱") || symbol.GetFamilyNameBySymbol().Contains("住宅家居配电箱") || (familyInstance.GetParameter("名称") != null && familyInstance.GetParameter("名称").AsString().Contains("家居配电箱")) || (symbol.GetParameter("名称") != null && symbol.GetParameter("名称").AsString().Contains("家居配电箱")))
						{
							list.Add(element);
						}
					}
				}
			}
			return list;
		}

		private void LastConnAreas(List<RoomLightEquipment> roomLightEquipments, List<RoomLightEquipment> lastConnArea, List<List<RoomLightEquipment>> connLamps)
		{
			foreach (RoomLightEquipment item in lastConnArea)
			{
				if (roomLightEquipments.Contains(item))
				{
					roomLightEquipments.Remove(item);
				}
			}
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			List<RoomLightEquipment> list2 = new List<RoomLightEquipment>();
			List<string> roomName = this.GetRoomName(6);
			string text = "";
			foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
			{
				if (roomLightEquipment.Lamps != null)
				{
					if (roomLightEquipment.Room.GetParameter("名称") != null)
					{
						text = roomLightEquipment.Room.GetParameter("名称").AsString();
					}
					bool flag = true;
					foreach (string value in roomName)
					{
						if (text.Contains(value))
						{
							flag = false;
							list.Add(roomLightEquipment);
							break;
						}
					}
					if (flag)
					{
						list2.Add(roomLightEquipment);
					}
				}
			}
			foreach (RoomLightEquipment roomLightEquipment2 in lastConnArea)
			{
				if (!roomLightEquipment2.IsConn)
				{
					foreach (List<RoomLightEquipment> list3 in connLamps)
					{
						RoomLightEquipment value2 = new RoomLightEquipment();
						if (list3[1].Room.Id == roomLightEquipment2.Room.Id)
						{
							value2 = list3[1];
							list3[1] = list3[0];
							list3[0] = value2;
						}
						if (list3[0].Room.Id == roomLightEquipment2.Room.Id)
						{
							foreach (RoomLightEquipment roomLightEquipment3 in lastConnArea)
							{
								if (list3[1].Room.Id == roomLightEquipment3.Room.Id)
								{
									roomLightEquipment3.IsConn = true;
								}
							}
						}
					}
					List<Element> list4 = this.listFilterRooms[roomLightEquipment2.Room];
					List<RoomLightEquipment> list5 = new List<RoomLightEquipment>();
					foreach (RoomLightEquipment roomLightEquipment4 in list2)
					{
						foreach (Element element in list4)
						{
							if (roomLightEquipment4.Room.Id == element.Id)
							{
								list5.Add(roomLightEquipment4);
							}
						}
					}
					if (list5.Count != 0)
					{
						bool flag2 = false;
						using (List<RoomLightEquipment>.Enumerator enumerator4 = list5.GetEnumerator())
						{
							while (enumerator4.MoveNext())
							{
								if (enumerator4.Current.ConnectNumber < 4)
								{
									flag2 = true;
									break;
								}
							}
						}
						if (flag2)
						{
							list2 = list5;
						}
					}
					Element lamps = roomLightEquipment2.Lamps;
					LocationPoint locationPoint = lamps.Location as LocationPoint;
					XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
					SortedDictionary<double, RoomLightEquipment> sortedDictionary = this.SortOfDistance(list2, point);
					while (sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.ConnectNumber > 3)
					{
						sortedDictionary.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Key);
						if (sortedDictionary == null && sortedDictionary.Count == 0)
						{
							break;
						}
					}
					if (sortedDictionary != null && sortedDictionary.Count != 0)
					{
						List<Element> list6 = new List<Element>();
						Element lamps2 = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.Lamps;
						list6.Add(lamps2);
						list6.Add(lamps);
						this.listConnectWires.Add(list6);
						RoomLightEquipment value3 = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value;
						int connectNumber = value3.ConnectNumber;
						value3.ConnectNumber = connectNumber + 1;
						RoomLightEquipment roomLightEquipment5 = roomLightEquipment2;
						connectNumber = roomLightEquipment5.ConnectNumber;
						roomLightEquipment5.ConnectNumber = connectNumber + 1;
						sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.IsConn = true;
						roomLightEquipment2.IsConn = true;
					}
				}
			}
		}

		private List<RoomLightEquipment> CommonAreaLampsConnectLine(List<RoomLightEquipment> listCommonArea, Element element)
		{
			LocationPoint locationPoint = element.Location as LocationPoint;
			XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
			SortedDictionary<double, RoomLightEquipment> sortedDictionary = this.SortOfDistance(listCommonArea, point);
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			if (sortedDictionary != null && sortedDictionary.Count != 0)
			{
				List<Element> list2 = new List<Element>();
				Element lamps = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.Lamps;
				list2.Add(lamps);
				list2.Add(element);
				IEnumerable<ElementId> allFamilyInstance = this.GetAllFamilyInstance(base.Doc, BuiltInCategory.OST_Wire);
				EquipmentConnectWires.CreatWireByElementList(list2, base.Doc);
				this.listConnectWires.Add(list2);
				ICollection<ElementId> collection = null;
				collection = this.GetAllFamilyInstance(base.Doc, BuiltInCategory.OST_Wire);
				new List<ElementId>();
				foreach (ElementId item in allFamilyInstance.Intersect(collection))
				{
					collection.Remove(item);
				}
				this.DrawMarkLine(element, lamps, collection);
				RoomLightEquipment value = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value;
				int connectNumber = value.ConnectNumber;
				value.ConnectNumber = connectNumber + 1;
				sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.IsConn = true;
				list.Add(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value);
				sortedDictionary.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Key);
			}
			return this.CommonAreaConnect(sortedDictionary, list);
		}

		private ICollection<ElementId> GetAllFamilyInstance(Autodesk.Revit.DB.Document doc, BuiltInCategory builtInCategory)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			new ElementOwnerViewFilter(doc.ActiveView.Id);
			return filteredElementCollector.OfCategory(builtInCategory).ToElementIds();
		}

		private XYZ GetWireMidPoint(Wire wire)
		{
			List<Connector> wireConnectors = RevitUtils.GetWireConnectors(wire);
			if (wireConnectors != null && wireConnectors.Count == 2)
			{
				XYZ point = (wireConnectors[0].Origin + wireConnectors[1].Origin) / 2.0;
				List<Curve> wireCurve = RevitVersionFuncs.GetWireCurve(wire, base.Doc.ActiveView);
				Curve curve = wireCurve[0];
				foreach (Curve curve2 in wireCurve)
				{
					YJKLine hyline = new YJKLine(curve2.GetEndPoint(0), curve2.GetEndPoint(1));
					hyline.MakeUnBound();
					XYZ project = hyline.GetProject(point);
					if (hyline.IsPointOnLine(project))
					{
						return project;
					}
					if (curve2.Length > curve.Length)
					{
						curve = curve2;
					}
				}
				return (curve.GetEndPoint(0) + curve.GetEndPoint(1)) / 2.0;
			}
			return null;
		}

		private void DrawMarkLine(Element element, Element lamp, ICollection<ElementId> collectionId)
		{
			Wire wire = null;
			if (collectionId.Count > 0)
			{
				wire = (base.Doc.GetElementById(collectionId.First<ElementId>()) as Wire);
			}
			XYZ wireMidPoint = this.GetWireMidPoint(wire);
			LocationPoint locationPoint = element.Location as LocationPoint;
			XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
			LocationPoint locationPoint2 = lamp.Location as LocationPoint;
			XYZ xyz2 = new XYZ(locationPoint2.Point.X, locationPoint2.Point.Y, locationPoint2.Point.Z);
			XYZ xyz3 = new XYZ();
			if (xyz.X < xyz2.X && xyz.Y < xyz2.Y)
			{
				xyz3 = new XYZ(-1.0, 1.0, 0.0);
			}
			else if (xyz.X > xyz2.X && xyz.Y < xyz2.Y)
			{
				xyz3 = new XYZ(1.0, 1.0, 0.0);
			}
			else if (xyz.X < xyz2.X && xyz.Y > xyz2.Y)
			{
				xyz3 = new XYZ(-1.0, -1.0, 0.0);
			}
			else if (xyz.X > xyz2.X && xyz.Y > xyz2.Y)
			{
				xyz3 = new XYZ(1.0, -1.0, 0.0);
			}
			XYZ xyz4 = wireMidPoint + xyz3 * 2.0;
			Guid schemaGuid = new Guid("356cda5a-e6c5-4c2f-a9ef-b1113131b8c8");
			Transaction transaction = new Transaction(base.Doc, "LayLabel");
			try
			{
				transaction.Start();
				string pathName = Path.Combine(Product.DataLocation, "YArchitech Electric Shared Parameters.txt");
				string gropeName = "电气数据";
				List<string> listStr = new List<string>
				{
					"回路编号"
				};
				YJKRevitTools.SetInstanceParameter(base.UiApp, pathName, gropeName, listStr, BuiltInCategory.OST_Wire, true);
				Element element2 = wire;
				if (element2 == null)
				{
					transaction.RollBack();
				}
				ElementIdDate elementIdDate = ExtensibleStorageManager.ReadFromElement<ElementIdDate>(element2, schemaGuid);
				if (elementIdDate.GetId().IntegerValue != 0)
				{
					base.Doc.Delete(elementIdDate.GetId());
				}
				ElectricalUtils.DeleteTagOfElement(base.Doc, element2 as Wire, "导线回路标记");
				Family labelFamily = ElectricMark.WireLoopMark(base.Doc)[0].LoadInSubTrans(base.Doc);
				Autodesk.Revit.DB.ElementType leaderType = ElectricalUtils.GetLeaderType(base.Doc, PointStyle.Line, labelFamily);
				ElementSet elementSet = new ElementSet();
				if (leaderType != null)
				{
					if (xyz4 == null || xyz4.DistanceTo(wireMidPoint) < 0.005)
					{
						transaction.RollBack();
					}
					IndependentTag independentTag = RevitVersionFuncs.NewTag(base.Doc, base.Doc.ActiveView, new Reference(element2), false, 0, 0, xyz4);
					independentTag.ChangeTypeId(leaderType.Id);
                    independentTag.LeaderEndCondition = (LeaderEndCondition)1;
					BoundingBoxXYZ boundingBoxXYZ = independentTag.get_BoundingBox(base.Doc.ActiveView);
					XYZ xyz5 = new XYZ(boundingBoxXYZ.Min.X, boundingBoxXYZ.Min.Y, wireMidPoint.Z);
					double num = 0.5;
					XYZ ePoint;
					if (Geometry.LessThan(xyz4.X, wireMidPoint.X))
					{
						ePoint = new XYZ(2.0 * boundingBoxXYZ.Min.X - boundingBoxXYZ.Max.X - 1.0, boundingBoxXYZ.Min.Y, wireMidPoint.Z);
						num = 2.0 * boundingBoxXYZ.Min.X - boundingBoxXYZ.Max.X - xyz4.X - 0.5;
					}
					else
					{
						ePoint = new XYZ(boundingBoxXYZ.Max.X + 1.0, boundingBoxXYZ.Min.Y, wireMidPoint.Z);
					}
					DetailLine detailLine = YJKRevitTools.DrawDetailLine(base.Doc, wireMidPoint, xyz5);
					DetailLine detailLine2 = YJKRevitTools.DrawDetailLine(base.Doc, xyz5, ePoint);
					elementSet.Insert(detailLine);
					elementSet.Insert(detailLine2);
					Line curve = ElectricalUtils.PromtLine(wireMidPoint, element2 as Wire);
					XYZ xyz6 = (curve.GetEndPoint(1) - curve.GetEndPoint(0)).Normalize();
					xyz6 = new XYZ(xyz6.X, xyz6.Y, 0.0);
					(xyz5 - wireMidPoint).Normalize();
					int num2 = ElectricalUtils.Quadrant(wireMidPoint, xyz4);
					double num3;
					if (num2 == 1 || num2 == 3)
					{
						num3 = 2.3561944901923448 + xyz6.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
					}
					else
					{
						num3 = 0.78539816339744828 + xyz6.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
					}
					DetailLine detailLine3 = YJKRevitTools.DrawDetailLine(base.Doc, wireMidPoint, wireMidPoint + xyz6);
					DetailLine detailLine4 = YJKRevitTools.DrawDetailLine(base.Doc, wireMidPoint, wireMidPoint - xyz6);
					Line line = Line.CreateBound(wireMidPoint, new XYZ(wireMidPoint.X, wireMidPoint.Y, wireMidPoint.Z + 1.0));
					ElementTransformUtils.RotateElement(base.Doc, detailLine3.Id, line, num3);
					ElementTransformUtils.RotateElement(base.Doc, detailLine4.Id, line, num3);
					elementSet.Insert(detailLine3);
					elementSet.Insert(detailLine4);
					base.Doc.CreatYJKGroup(elementSet);
					ElementTransformUtils.MoveElement(base.Doc, independentTag.Id, new XYZ(num, 0.0, 0.0));
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private List<RoomLightEquipment> CommonAreaConnect(SortedDictionary<double, RoomLightEquipment> dic, List<RoomLightEquipment> lists)
		{
			if (dic != null && dic.Count != 0)
			{
				List<Element> list = new List<Element>();
				Element lamps = dic.First<KeyValuePair<double, RoomLightEquipment>>().Value.Lamps;
				LocationPoint locationPoint = lamps.Location as LocationPoint;
				XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
				foreach (KeyValuePair<double, RoomLightEquipment> keyValuePair in this.SortOfDistance(lists, point))
				{
					if (keyValuePair.Value.ConnectNumber <= 2)
					{
						list.Add(lamps);
						list.Add(keyValuePair.Value.Lamps);
						this.listConnectWires.Add(list);
						RoomLightEquipment value = keyValuePair.Value;
						int connectNumber = value.ConnectNumber;
						value.ConnectNumber = connectNumber + 1;
						RoomLightEquipment value2 = dic.First<KeyValuePair<double, RoomLightEquipment>>().Value;
						connectNumber = value2.ConnectNumber;
						value2.ConnectNumber = connectNumber + 1;
						dic.First<KeyValuePair<double, RoomLightEquipment>>().Value.IsConn = true;
						keyValuePair.Value.IsConn = true;
						lists.Add(dic.First<KeyValuePair<double, RoomLightEquipment>>().Value);
						dic.Remove(dic.First<KeyValuePair<double, RoomLightEquipment>>().Key);
						break;
					}
				}
				return this.CommonAreaConnect(dic, lists);
			}
			return lists;
		}

		private List<RoomLightEquipment> OtherAreaConnect(List<RoomLightEquipment> Equipments, List<RoomLightEquipment> listOtherAreas, List<List<RoomLightEquipment>> connLamps, List<RoomLightEquipment> LastConnArea)
		{
			if (listOtherAreas == null || listOtherAreas.Count == 0 || Equipments.Count == 0)
			{
				return Equipments;
			}
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			List<RoomLightEquipment> list2 = new List<RoomLightEquipment>();
			foreach (RoomLightEquipment roomLightEquipment in listOtherAreas)
			{
				if (roomLightEquipment.IsConn)
				{
					list.Add(roomLightEquipment);
				}
				else
				{
					list2.Add(roomLightEquipment);
				}
			}
			foreach (RoomLightEquipment roomLightEquipment2 in Equipments)
			{
				if (roomLightEquipment2.ConnectNumber <= 3)
				{
					List<Element> list3 = this.listFilterRooms[roomLightEquipment2.Room];
					List<RoomLightEquipment> list4 = new List<RoomLightEquipment>();
					foreach (RoomLightEquipment roomLightEquipment3 in list2)
					{
						foreach (Element element in list3)
						{
							if (roomLightEquipment3.Room.Id == element.Id)
							{
								list4.Add(roomLightEquipment3);
							}
						}
					}
					if (list4.Count != 0)
					{
						LocationPoint locationPoint = roomLightEquipment2.Lamps.Location as LocationPoint;
						XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
						SortedDictionary<double, RoomLightEquipment> sortedDictionary = this.SortOfDistance(list4, point);
						int connectNumber = roomLightEquipment2.ConnectNumber;
						while (connectNumber < 4 && sortedDictionary != null && sortedDictionary.Count != 0)
						{
							if (sortedDictionary != null && sortedDictionary.Count != 0 && sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.IsConn)
							{
								list.Add(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value);
								list2.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value);
								sortedDictionary.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Key);
							}
							else
							{
								foreach (List<RoomLightEquipment> list5 in connLamps)
								{
									RoomLightEquipment value = new RoomLightEquipment();
									if (list5[1].Room.Id == sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.Room.Id)
									{
										value = list5[1];
										list5[1] = list5[0];
										list5[0] = value;
									}
									if (list5[0].Room.Id == sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.Room.Id)
									{
										RoomLightEquipment value2 = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value;
										int connectNumber2 = value2.ConnectNumber;
										value2.ConnectNumber = connectNumber2 + 1;
										foreach (RoomLightEquipment roomLightEquipment4 in list2)
										{
											if (roomLightEquipment4.Room.Id == list5[1].Room.Id)
											{
												RoomLightEquipment roomLightEquipment5 = roomLightEquipment4;
												connectNumber2 = roomLightEquipment5.ConnectNumber;
												roomLightEquipment5.ConnectNumber = connectNumber2 + 1;
												roomLightEquipment4.IsConn = true;
											}
										}
										foreach (KeyValuePair<double, RoomLightEquipment> keyValuePair in sortedDictionary)
										{
											if (keyValuePair.Value.Room.Id == list5[1].Room.Id)
											{
												RoomLightEquipment value3 = keyValuePair.Value;
												connectNumber2 = value3.ConnectNumber;
												value3.ConnectNumber = connectNumber2 + 1;
												keyValuePair.Value.IsConn = true;
											}
										}
										foreach (RoomLightEquipment roomLightEquipment6 in LastConnArea)
										{
											if (roomLightEquipment6.Room.Id == list5[1].Room.Id)
											{
												RoomLightEquipment roomLightEquipment7 = roomLightEquipment6;
												connectNumber2 = roomLightEquipment7.ConnectNumber;
												roomLightEquipment7.ConnectNumber = connectNumber2 + 1;
												roomLightEquipment6.IsConn = true;
											}
										}
									}
								}
								if (sortedDictionary != null && sortedDictionary.Count != 0 && this.JudgeRoomNearOtherArea(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value, list))
								{
									RoomLightEquipment value4 = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value;
									int connectNumber2 = value4.ConnectNumber;
									value4.ConnectNumber = connectNumber2 + 1;
									sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.IsConn = true;
									list.Add(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value);
									list2.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value);
									sortedDictionary.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Key);
								}
								else
								{
									if (sortedDictionary == null || sortedDictionary.Count == 0)
									{
										break;
									}
									List<Element> list6 = new List<Element>();
									Element lamps = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.Lamps;
									list6.Add(lamps);
									list6.Add(roomLightEquipment2.Lamps);
									this.listConnectWires.Add(list6);
									RoomLightEquipment roomLightEquipment8 = roomLightEquipment2;
									int connectNumber2 = roomLightEquipment8.ConnectNumber;
									roomLightEquipment8.ConnectNumber = connectNumber2 + 1;
									RoomLightEquipment value5 = sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value;
									connectNumber2 = value5.ConnectNumber;
									value5.ConnectNumber = connectNumber2 + 1;
									roomLightEquipment2.IsConn = true;
									sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value.IsConn = true;
									list.Add(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value);
									list2.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Value);
									sortedDictionary.Remove(sortedDictionary.First<KeyValuePair<double, RoomLightEquipment>>().Key);
								}
							}
							connectNumber = roomLightEquipment2.ConnectNumber;
						}
					}
				}
			}
			List<RoomLightEquipment> list7 = this.OtherAreaConnect(list, list2, connLamps, LastConnArea);
			list7.AddRange(Equipments);
			return list7;
		}

		private bool JudgeRoomNearOtherArea(RoomLightEquipment roomEquip, List<RoomLightEquipment> Equipments)
		{
			List<Element> list = this.listFilterRooms[roomEquip.Room];
			List<RoomLightEquipment> list2 = new List<RoomLightEquipment>();
			foreach (RoomLightEquipment roomLightEquipment in Equipments)
			{
				foreach (Element element in list)
				{
					if (roomLightEquipment.Room.Id == element.Id)
					{
						list2.Add(roomLightEquipment);
					}
				}
			}
			if (list2.Count == 0)
			{
				return false;
			}
			List<RoomLightEquipment> list3 = new List<RoomLightEquipment>();
			List<RoomLightEquipment> list4 = new List<RoomLightEquipment>();
			List<string> roomName = this.GetRoomName(6);
			string text = "";
			foreach (RoomLightEquipment roomLightEquipment2 in list2)
			{
				if (roomLightEquipment2.Lamps != null)
				{
					if (roomLightEquipment2.Room.GetParameter("名称") != null)
					{
						text = roomLightEquipment2.Room.GetParameter("名称").AsString();
					}
					bool flag = true;
					foreach (string value in roomName)
					{
						if (text.Contains(value))
						{
							flag = false;
							list3.Add(roomLightEquipment2);
							break;
						}
					}
					if (flag)
					{
						list4.Add(roomLightEquipment2);
					}
				}
			}
			if (list4.Count != 0)
			{
				foreach (RoomLightEquipment roomLightEquipment3 in list4)
				{
					if (roomLightEquipment3.IsConn && roomLightEquipment3.ConnectNumber < 4)
					{
						foreach (RoomLightEquipment roomLightEquipment4 in Equipments)
						{
							if (roomLightEquipment4.Room.Id == roomLightEquipment3.Room.Id)
							{
								List<Element> list5 = new List<Element>();
								list5.Add(roomLightEquipment4.Lamps);
								list5.Add(roomEquip.Lamps);
								this.listConnectWires.Add(list5);
								RoomLightEquipment roomLightEquipment5 = roomLightEquipment4;
								int connectNumber = roomLightEquipment5.ConnectNumber;
								roomLightEquipment5.ConnectNumber = connectNumber + 1;
								return true;
							}
						}
					}
				}
				return false;
			}
			return false;
		}

		private Dictionary<Room, List<Element>> GetRoomAdjoinRooms(List<Room> listRoom)
		{
			Dictionary<Room, List<Element>> dictionary = new Dictionary<Room, List<Element>>();
			foreach (Room room in listRoom)
			{
				List<Element> value = this.FilterRooms(room);
				dictionary.Add(room, value);
			}
			return dictionary;
		}

		private List<Element> FilterRooms(Room room)
		{
			List<Curve> list = this.listCurves[room];
			if (list.Count == 4)
			{
				return this.FilterRoomss(room);
			}
			if (list.Count == 0)
			{
				return null;
			}
			bool flag = false;
			List<Curve> list2 = GroundStorehouseNozzle.MergeCurve(GroundStorehouseNozzle.SortListCurves(list, out flag));
			if (list2.Count == 4)
			{
				return this.FilterRoomss(room);
			}
			List<Element> list3 = new List<Element>();
			foreach (Curve curve in list2)
			{
				if (curve != null)
				{
					List<Element> listElement = new List<Element>();
					XYZ endPoint = curve.GetEndPoint(0);
					XYZ endPoint2 = curve.GetEndPoint(1);
					XYZ xyz = endPoint - endPoint2;
					XYZ xyz2 = new XYZ(0.0, 0.0, 1.0);
					XYZ xyz3 = xyz.CrossProduct(xyz2).Normalize();
					XYZ point = endPoint + xyz3 * 2.0;
					XYZ point2 = endPoint2 - xyz3 * 2.0;
					listElement = this.GetElementList(point, point2, BuiltInCategory.OST_Rooms);
					list3 = this.AddElement(list3, listElement);
				}
			}
			list3 = this.RemoveListElement(list3, room);
			return list3;
		}

		private List<Element> FilterRoomss(Room room)
		{
			BoundingBoxXYZ boundingBoxXYZ = room.get_BoundingBox(base.Doc.ActiveView);
			XYZ point = new XYZ(boundingBoxXYZ.Min.X - 2.0, boundingBoxXYZ.Min.Y, boundingBoxXYZ.Min.Z);
			XYZ point2 = new XYZ(boundingBoxXYZ.Max.X + 2.0, boundingBoxXYZ.Max.Y, boundingBoxXYZ.Max.Z);
			List<Element> listElement = this.GetElementList(point2, point, BuiltInCategory.OST_Rooms);
			point = new XYZ(boundingBoxXYZ.Min.X, boundingBoxXYZ.Min.Y - 2.0, boundingBoxXYZ.Min.Z);
			point2 = new XYZ(boundingBoxXYZ.Max.X, boundingBoxXYZ.Max.Y + 2.0, boundingBoxXYZ.Max.Z);
			List<Element> elementList = this.GetElementList(point2, point, BuiltInCategory.OST_Rooms);
			listElement = this.AddElement(listElement, elementList);
			return this.RemoveListElement(listElement, room);
		}

		private SortedDictionary<double, RoomLightEquipment> SortOfDistance(List<RoomLightEquipment> roomLightEquipments, XYZ point)
		{
			SortedDictionary<double, RoomLightEquipment> sortedDictionary = new SortedDictionary<double, RoomLightEquipment>();
			foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
			{
				if (roomLightEquipment.Lamps != null)
				{
					LocationPoint locationPoint = roomLightEquipment.Lamps.Location as LocationPoint;
					double key = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z).DistanceTo(point);
					sortedDictionary.Add(key, roomLightEquipment);
				}
			}
			return sortedDictionary;
		}

		private bool IsElementParallel(Element elem1, Element elem2)
		{
			LocationCurve locationCurve = elem1.Location as LocationCurve;
			LocationCurve locationCurve2 = elem2.Location as LocationCurve;
			if (locationCurve != null && locationCurve2 != null)
			{
				YJKLine hyline = new YJKLine(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1));
				YJKLine hyline2 = new YJKLine(locationCurve2.Curve.GetEndPoint(0), locationCurve2.Curve.GetEndPoint(1));
				if (hyline.IsParallel(hyline2.Line))
				{
					return true;
				}
			}
			return false;
		}

		private XYZ GetLightLayPoint(Room room, FamilyInstance fi, XYZ doorXYZ, XYZ vector, Curve wallCurve, bool OutsideRoomLay = false)
		{
			List<Element> list = this.GetWallList(doorXYZ, doorXYZ + vector);
			list = this.RemoveListElement(list, fi.Host);
			if (list.Count != 1)
			{
				return null;
			}
			Element element = list[0];
			List<Curve> curves = this.listCurves[room];
			Curve curve = null;
			if (OutsideRoomLay)
			{
				curve = this.GetWallCurve(curves, element, doorXYZ + vector);
			}
			else
			{
				curve = this.GetWallCurve(curves, element, null);
			}
			if (this.IsElementParallel(fi.Host, element))
			{
				LocationCurve locationCurve = element.Location as LocationCurve;
				if (locationCurve != null)
				{
					XYZ point = (locationCurve.Curve.GetEndPoint(0) + locationCurve.Curve.GetEndPoint(1)) / 2.0;
					curve = this.GetWallCurve(curves, element, point);
					if (curve == null)
					{
						return null;
					}
					double num = fi.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble() * 304.8;
					if (curve.Length > 0.98425196850393692)
					{
						return doorXYZ + vector.Normalize() * (200.0 + num / 2.0) / 304.8;
					}
					if (curve.Length > 0.65616797900262469 && curve.Length < 0.98425196850393692)
					{
						return doorXYZ + vector.Normalize() * (curve.Length * 304.8 + num) / 609.6;
					}
					XYZ searchPoint = (locationCurve.Curve.GetEndPoint(0).DistanceTo(doorXYZ) < locationCurve.Curve.GetEndPoint(1).DistanceTo(doorXYZ)) ? locationCurve.Curve.GetEndPoint(1) : locationCurve.Curve.GetEndPoint(0);
					List<Element> list2 = this.GetElements(searchPoint, 0.1);
					list2 = this.RemoveListElement(list2, element);
					if (list2.Count != 1)
					{
						return null;
					}
					curve = this.GetWallCurve(curves, list2[0], null);
				}
			}
			if (curve == null)
			{
				return null;
			}
			List<Element> elementList = this.GetElementList(curve.GetEndPoint(0), curve.GetEndPoint(1), BuiltInCategory.OST_Doors);
			XYZ result = new XYZ();
			Element element2 = null;
			foreach (Element element3 in elementList)
			{
				if ((element3 as FamilyInstance).Host.Id == element.Id)
				{
					element2 = element3;
				}
			}
			if (element2 != null)
			{
				result = this.GetLayPoint2(wallCurve, curve, element2);
			}
			else
			{
				result = this.GetLayPoint1(wallCurve, curve);
			}
			return result;
		}

		private XYZ GetLightOutLayPoint(RoomLightEquipment roomLightEquipment, FamilyInstance fi, XYZ doorXYZ, XYZ vector, Curve wallCurve, ref int InOrOutLaySwitch, XYZ otherVector = null)
		{
			Room room = roomLightEquipment.Room;
			List<Element> list = this.GetWallList(doorXYZ, doorXYZ + vector);
			list = this.RemoveListElement(list, fi.Host);
			if (list.Count != 1)
			{
				LocationCurve locationCurve = fi.Host.Location as LocationCurve;
				YJKLine hyline = new YJKLine(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1));
				LocationPoint locationPoint = roomLightEquipment.Room.Location as LocationPoint;
				XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
				XYZ project = hyline.GetProject(xyz);
				XYZ xyz2 = xyz - project;
				XYZ xyz3 = new XYZ(xyz2.X, xyz2.Y, 0.0);
				Curve curve = Line.CreateBound(doorXYZ + vector, doorXYZ + vector + xyz3);
				Wall wall = fi.Host as Wall;
				double num = 0.0;
				if (wall != null)
				{
					num = wall.WallType.GetParameter("厚度").AsDouble() * 304.8;
				}
				XYZ xyz4 = new XYZ();
				xyz4 = this.GetOutLayPoint1(wallCurve, curve, num, num, roomLightEquipment);
				if (xyz4 == null && otherVector != null)
				{
					xyz4 = this.GetLightOutLayPoint(roomLightEquipment, fi, doorXYZ, otherVector, wallCurve, ref InOrOutLaySwitch, null);
				}
				return xyz4;
			}
			Element element = list[0];
			List<Curve> curves = this.listCurves[room];
			Curve wallCurve2 = this.GetWallCurve(curves, element, doorXYZ + vector);
			if (this.IsElementParallel(fi.Host, element))
			{
				LocationCurve locationCurve2 = element.Location as LocationCurve;
				if (locationCurve2 != null)
				{
					XYZ point = (locationCurve2.Curve.GetEndPoint(0) + locationCurve2.Curve.GetEndPoint(1)) / 2.0;
					wallCurve2 = this.GetWallCurve(curves, element, point);
					if (wallCurve2 == null)
					{
						return null;
					}
					double num2 = fi.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble() * 304.8;
					if (wallCurve2.Length > 0.98425196850393692)
					{
						InOrOutLaySwitch = -1;
						return doorXYZ + vector.Normalize() * (200.0 + num2 / 2.0) / 304.8;
					}
					if (wallCurve2.Length > 0.65616797900262469 && wallCurve2.Length < 0.98425196850393692)
					{
						InOrOutLaySwitch = -1;
						return doorXYZ + vector.Normalize() * (wallCurve2.Length * 304.8 + num2) / 609.6;
					}
					XYZ searchPoint = (locationCurve2.Curve.GetEndPoint(0).DistanceTo(doorXYZ) < locationCurve2.Curve.GetEndPoint(1).DistanceTo(doorXYZ)) ? locationCurve2.Curve.GetEndPoint(1) : locationCurve2.Curve.GetEndPoint(0);
					List<Element> list2 = this.GetElements(searchPoint, 0.1);
					list2 = this.RemoveListElement(list2, element);
					if (list2.Count != 1)
					{
						return null;
					}
					wallCurve2 = this.GetWallCurve(curves, list2[0], null);
				}
			}
			if (wallCurve2 == null)
			{
				return null;
			}
			XYZ xyz5 = new XYZ();
			XYZ vector2 = this.GetVector(wallCurve, wallCurve2, ref xyz5);
			Curve curve2 = (element.Location as LocationCurve).Curve;
			YJKLine hyline2 = new YJKLine(xyz5, xyz5 + vector2);
			hyline2.MakeUnBound();
			XYZ project2 = hyline2.GetProject(curve2.GetEndPoint(0));
			hyline2.GetProject(curve2.GetEndPoint(1));
			XYZ point2 = new XYZ();
			if ((project2 - xyz5).Normalize().IsAlmostEqualTo(vector2.Normalize(), 0.001))
			{
				point2 = curve2.GetEndPoint(0);
			}
			else
			{
				point2 = curve2.GetEndPoint(1);
			}
			List<Element> elementList = this.GetElementList(xyz5, point2, BuiltInCategory.OST_Doors);
			XYZ xyz6 = new XYZ();
			Element element2 = null;
			foreach (Element element3 in elementList)
			{
				if ((element3 as FamilyInstance).Host.Id == element.Id)
				{
					element2 = element3;
				}
			}
			Wall wall2 = fi.Host as Wall;
			double thickness = 0.0;
			if (wall2 != null)
			{
				thickness = wall2.WallType.GetParameter("厚度").AsDouble() * 304.8;
			}
			Wall wall3 = element as Wall;
			double thickness2 = 0.0;
			if (wall3 != null)
			{
				thickness2 = wall3.WallType.GetParameter("厚度").AsDouble() * 304.8;
			}
			if (element2 != null)
			{
				xyz6 = this.GetOutLayPoint2(wallCurve, wallCurve2, element2, thickness, thickness2, roomLightEquipment);
			}
			else
			{
				xyz6 = this.GetOutLayPoint1(wallCurve, wallCurve2, thickness, thickness2, roomLightEquipment);
			}
			if (xyz6 == null && otherVector != null)
			{
				xyz6 = this.GetLightOutLayPoint(roomLightEquipment, fi, doorXYZ, otherVector, wallCurve, ref InOrOutLaySwitch, null);
			}
			return xyz6;
		}

		private List<Element> RemoveListElement(List<Element> listElement, Element elem)
		{
			List<Element> list = new List<Element>();
			foreach (Element element in listElement)
			{
				if (element.Id != elem.Id)
				{
					list.Add(element);
				}
			}
			return list;
		}

		private List<Element> AddElement(List<Element> listElement, List<Element> listElement2)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in listElement)
			{
				list.Add(element.Id);
			}
			foreach (Element element2 in listElement2)
			{
				if (!list.Contains(element2.Id))
				{
					listElement.Add(element2);
					list.Add(element2.Id);
				}
			}
			return listElement;
		}

		private XYZ GetHandOrientation(FamilyInstance fInstance)
		{
			IEnumerator<GeometryObject> enumerator = fInstance.get_Geometry(new Options
			{
				View = base.Doc.ActiveView,
				ComputeReferences = true
			}).GetEnumerator();
			enumerator.Reset();
			XYZ result = new XYZ();
			while (enumerator.MoveNext())
			{
				GeometryObject geometryObject = enumerator.Current;
				if (geometryObject is GeometryInstance)
				{
					foreach (GeometryObject geometryObject2 in (geometryObject as GeometryInstance).GetInstanceGeometry())
					{
						if (geometryObject2 is Arc)
						{
							Arc arc = geometryObject2 as Arc;
							if (!(arc == null))
							{
								LocationPoint locationPoint = fInstance.Location as LocationPoint;
								XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
								XYZ end = xyz + fInstance.HandOrientation;
								YJKLine hyline = new YJKLine(xyz, end);
								hyline.MakeUnBound();
								XYZ xyz2 = new XYZ();
								XYZ xyz3 = new XYZ();
								XYZ xyz4 = new XYZ(arc.GetEndPoint(0).X, arc.GetEndPoint(0).Y, xyz.Z);
								XYZ xyz5 = new XYZ(arc.GetEndPoint(1).X, arc.GetEndPoint(1).Y, xyz.Z);
								if (hyline.IsPointOnCurveLine(xyz4))
								{
									xyz2 = xyz4;
									xyz3 = hyline.GetProject(xyz5);
								}
								else
								{
									xyz2 = xyz5;
									xyz3 = hyline.GetProject(xyz4);
								}
								result = (xyz3 - xyz2).Normalize();
							}
						}
					}
				}
			}
			return result;
		}

		private RoomLightEquipment LaySwichInRoom(RoomLightEquipment roomLightEquipment)
		{
			roomLightEquipment.SwitchType = 1;
			if (roomLightEquipment.Doors.Count == 0)
			{
				return null;
			}
			FamilyInstance familyInstance = roomLightEquipment.Doors[0] as FamilyInstance;
			FamilySymbol symbol = familyInstance.Symbol;
			string familyName = symbol.FamilyName;
			string name = symbol.Name;
			double num = symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble() * 304.8;
			LocationPoint locationPoint = roomLightEquipment.Doors[0].Location as LocationPoint;
			XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			Element host = familyInstance.Host;
			List<Curve> curves = this.listCurves[roomLightEquipment.Room];
			Curve wallCurve = this.GetWallCurve(curves, host, null);
			if (wallCurve == null)
			{
				return null;
			}
			XYZ endPoint = wallCurve.GetEndPoint(0);
			XYZ endPoint2 = wallCurve.GetEndPoint(1);
			YJKLine hyline = new YJKLine(endPoint, endPoint2);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(point);
			if (familyName.Contains("推拉门") || familyName.Contains("子母门") || name.Contains("ZMM") || num >= 1200.0)
			{
				RoomLightEquipmentWallInfo wallInfo = this.GetWallInfo(roomLightEquipment, false);
				if (wallInfo == null)
				{
					return null;
				}
				if (wallInfo.LeftLength < 200.0)
				{
					if (this.IsWallHaveParllelWall(familyInstance, project, wallInfo.LeftVector))
					{
						roomLightEquipment.Point = this.GetLightLayPoint(roomLightEquipment.Room, familyInstance, project, wallInfo.LeftVector, wallInfo.Curve, false);
						if (roomLightEquipment.Point != null)
						{
							return roomLightEquipment;
						}
					}
					if (wallInfo.RightLength < 200.0)
					{
						roomLightEquipment.Point = this.GetLightLayPoint(roomLightEquipment.Room, familyInstance, project, wallInfo.LeftVector, wallInfo.Curve, false);
					}
					else if (wallInfo.RightLength > 300.0)
					{
						roomLightEquipment.Point = project + wallInfo.RightVector.Normalize() * (200.0 + num / 2.0) / 304.8;
					}
					else
					{
						roomLightEquipment.Point = project + wallInfo.RightVector.Normalize() * (wallInfo.RightLength + num) / 609.6;
					}
				}
				else if (wallInfo.LeftLength > 300.0)
				{
					roomLightEquipment.Point = project + wallInfo.LeftVector.Normalize() * (200.0 + num / 2.0) / 304.8;
				}
				else
				{
					roomLightEquipment.Point = project + wallInfo.LeftVector.Normalize() * (wallInfo.LeftLength + num) / 609.6;
				}
			}
			else
			{
				RoomLightEquipmentWallInfo wallInfo2 = this.GetWallInfo(roomLightEquipment, false);
				if (wallInfo2 == null)
				{
					return null;
				}
				XYZ handOrientation = this.GetHandOrientation(familyInstance);
				XYZ xyz = new XYZ();
				double num2;
				if (handOrientation.IsAlmostEqualTo(wallInfo2.LeftVector.Normalize(), 0.0001))
				{
					xyz = wallInfo2.RightVector;
					num2 = wallInfo2.RightLength;
				}
				else
				{
					xyz = wallInfo2.LeftVector;
					num2 = wallInfo2.LeftLength;
				}
				if (num2 < 200.0)
				{
					roomLightEquipment.Point = this.GetLightLayPoint(roomLightEquipment.Room, familyInstance, project, xyz, wallInfo2.Curve, false);
				}
				else if (num2 > 300.0)
				{
					roomLightEquipment.Point = project + xyz.Normalize() * (200.0 + num / 2.0) / 304.8;
				}
				else
				{
					roomLightEquipment.Point = project + xyz.Normalize() * (num2 + num) / 609.6;
				}
			}
			return roomLightEquipment;
		}

		private RoomLightEquipment LaySwichOutRoom(RoomLightEquipment roomLightEquipment)
		{
			roomLightEquipment.SwitchType = 2;
			foreach (string value in this.GetRoomName(8))
			{
				if (roomLightEquipment.Room.GetParameter("名称").AsString().Contains(value))
				{
					roomLightEquipment.SwitchType = 1;
				}
			}
			if (roomLightEquipment.Doors.Count == 0)
			{
				return null;
			}
			FamilyInstance familyInstance = roomLightEquipment.Doors[0] as FamilyInstance;
			FamilySymbol symbol = familyInstance.Symbol;
			string familyName = symbol.FamilyName;
			string name = symbol.Name;
			double num = symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble() * 304.8;
			LocationPoint locationPoint = roomLightEquipment.Doors[0].Location as LocationPoint;
			XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			Element host = familyInstance.Host;
			List<Curve> curves = this.listCurves[roomLightEquipment.Room];
			Curve wallCurve = this.GetWallCurve(curves, host, point);
			if (wallCurve == null)
			{
				return null;
			}
			XYZ endPoint = wallCurve.GetEndPoint(0);
			XYZ endPoint2 = wallCurve.GetEndPoint(1);
			XYZ project = new YJKLine(endPoint, endPoint2).GetProject(point);
			RoomLightEquipmentWallInfo wallInfo = this.GetWallInfo(roomLightEquipment, true);
			if (wallInfo == null)
			{
				return null;
			}
			bool flag = true;
			if (this.GetHandOrientation(familyInstance).IsAlmostEqualTo(wallInfo.LeftVector.Normalize(), 0.0001))
			{
				flag = false;
			}
			if (familyName.Contains("推拉门") || flag)
			{
				if (wallInfo.LeftLength < 200.0)
				{
					if (this.IsWallHaveParllelWall(familyInstance, project, wallInfo.LeftVector))
					{
						int inOrOutLaySwitch = 1;
						roomLightEquipment.Point = this.GetLightOutLayPoint(roomLightEquipment, familyInstance, project, wallInfo.LeftVector, wallInfo.Curve, ref inOrOutLaySwitch, wallInfo.RightVector);
						roomLightEquipment.InOrOutLaySwitch = inOrOutLaySwitch;
						if (roomLightEquipment.Point != null)
						{
							return roomLightEquipment;
						}
					}
					if (wallInfo.RightLength < 200.0)
					{
						int inOrOutLaySwitch2 = 1;
						roomLightEquipment.Point = this.GetLightOutLayPoint(roomLightEquipment, familyInstance, project, wallInfo.LeftVector, wallInfo.Curve, ref inOrOutLaySwitch2, wallInfo.RightVector);
						if (roomLightEquipment.Point == null)
						{
							inOrOutLaySwitch2 = -1;
							if (this.IsThereHaveWall(familyInstance, wallInfo.LeftVector, project))
							{
								roomLightEquipment.Point = this.GetLightLayPoint(roomLightEquipment.Room, familyInstance, project, wallInfo.LeftVector, wallInfo.Curve, true);
							}
							if (roomLightEquipment.Point == null && this.IsThereHaveWall(familyInstance, wallInfo.RightVector, project))
							{
								roomLightEquipment.Point = this.GetLightLayPoint(roomLightEquipment.Room, familyInstance, project, wallInfo.RightVector, wallInfo.Curve, true);
							}
						}
						roomLightEquipment.InOrOutLaySwitch = inOrOutLaySwitch2;
					}
					else if (wallInfo.RightLength > 300.0)
					{
						roomLightEquipment.Point = project + wallInfo.RightVector.Normalize() * (200.0 + num / 2.0) / 304.8;
					}
					else
					{
						roomLightEquipment.Point = project + wallInfo.RightVector.Normalize() * (wallInfo.RightLength + num) / 609.6;
					}
				}
				else if (wallInfo.LeftLength > 300.0)
				{
					roomLightEquipment.Point = project + wallInfo.LeftVector.Normalize() * (200.0 + num / 2.0) / 304.8;
				}
				else
				{
					roomLightEquipment.Point = project + wallInfo.LeftVector.Normalize() * (wallInfo.LeftLength + num) / 609.6;
				}
			}
			else if (wallInfo.RightLength < 200.0)
			{
				if (this.IsWallHaveParllelWall(familyInstance, project, wallInfo.RightVector))
				{
					int inOrOutLaySwitch3 = 1;
					roomLightEquipment.Point = this.GetLightOutLayPoint(roomLightEquipment, familyInstance, project, wallInfo.RightVector, wallInfo.Curve, ref inOrOutLaySwitch3, wallInfo.LeftVector);
					roomLightEquipment.InOrOutLaySwitch = inOrOutLaySwitch3;
					if (roomLightEquipment.Point != null)
					{
						return roomLightEquipment;
					}
				}
				if (wallInfo.LeftLength < 200.0)
				{
					int inOrOutLaySwitch4 = 1;
					roomLightEquipment.Point = this.GetLightOutLayPoint(roomLightEquipment, familyInstance, project, wallInfo.RightVector, wallInfo.Curve, ref inOrOutLaySwitch4, wallInfo.LeftVector);
					if (roomLightEquipment.Point == null)
					{
						inOrOutLaySwitch4 = -1;
						if (this.IsThereHaveWall(familyInstance, wallInfo.RightVector, project))
						{
							roomLightEquipment.Point = this.GetLightLayPoint(roomLightEquipment.Room, familyInstance, project, wallInfo.RightVector, wallInfo.Curve, true);
						}
						if (roomLightEquipment.Point == null && this.IsThereHaveWall(familyInstance, wallInfo.LeftVector, project))
						{
							roomLightEquipment.Point = this.GetLightLayPoint(roomLightEquipment.Room, familyInstance, project, wallInfo.LeftVector, wallInfo.Curve, true);
						}
					}
					roomLightEquipment.InOrOutLaySwitch = inOrOutLaySwitch4;
				}
				else if (wallInfo.LeftLength > 300.0)
				{
					roomLightEquipment.Point = project + wallInfo.LeftVector.Normalize() * (200.0 + num / 2.0) / 304.8;
				}
				else
				{
					roomLightEquipment.Point = project + wallInfo.LeftVector.Normalize() * (wallInfo.LeftLength + num) / 609.6;
				}
			}
			else if (wallInfo.RightLength > 300.0)
			{
				roomLightEquipment.Point = project + wallInfo.RightVector.Normalize() * (200.0 + num / 2.0) / 304.8;
			}
			else
			{
				roomLightEquipment.Point = project + wallInfo.RightVector.Normalize() * (wallInfo.RightLength + num) / 609.6;
			}
			return roomLightEquipment;
		}

		private bool IsThereHaveWall(FamilyInstance fi, XYZ vector, XYZ doorXYZ)
		{
			XYZ point = (fi.Location as LocationPoint).Point;
			List<Element> list = this.GetElements(doorXYZ + vector, 0.1);
			list = this.RemoveListElement(list, fi.Host);
			if (list != null && list.Count == 1)
			{
				Wall wall = list[0] as Wall;
				double num = 0.0;
				if (wall != null)
				{
					num = wall.WallType.GetParameter("厚度").AsDouble();
				}
				XYZ searchPoint = point + vector + vector.Normalize() * num * 2.0;
				List<Element> elements = this.GetElements(searchPoint, 0.01);
				return elements == null || elements.Count == 0;
			}
			return false;
		}

		private bool IsWallHaveParllelWall(FamilyInstance fi, XYZ doorXYZ, XYZ vector)
		{
			List<Element> list = this.GetWallList(doorXYZ, doorXYZ + vector);
			list = this.RemoveListElement(list, fi.Host);
			if (list.Count == 1)
			{
				Element elem = list[0];
				if (this.IsElementParallel(fi.Host, elem))
				{
					return true;
				}
			}
			return false;
		}

		private RoomLightEquipment LaySwitchByLine(RoomLightEquipment roomLightEquipment)
		{
			roomLightEquipment.SwitchType = 1;
			IEnumerable<IList<BoundarySegment>> boundarySegments = roomLightEquipment.Room.GetBoundarySegments(new SpatialElementBoundaryOptions());
			List<Element> list = new List<Element>();
			foreach (IList<BoundarySegment> source in boundarySegments)
			{
				list.AddRange(from segment in source
				select UnifiedModified.GetBoundarySegmentElement(base.Doc, segment));
			}
			foreach (Element element in list)
			{
				if (element is ModelLine)
				{
					List<Element> modelLine = this.GetModelLine(new List<Element>
					{
						element
					});
					List<Element> list2 = this.GetWallEquipment(modelLine);
					list2 = this.FilterWalls(list2, list);
					if (modelLine.Count == 0)
					{
						return null;
					}
					LocationCurve locationCurve = modelLine[0].Location as LocationCurve;
					XYZ point = (locationCurve.Curve.GetEndPoint(0) + locationCurve.Curve.GetEndPoint(1)) / 2.0;
					if (list2.Count > 1)
					{
						List<Element> list3 = this.JudgeLeftOrRight(list2, modelLine[0], roomLightEquipment.Room);
						roomLightEquipment.Point = this.GetLayPointByLine(list3, roomLightEquipment.Room, point);
					}
					else if (list2.Count == 1)
					{
						roomLightEquipment.Point = this.GetLayPointByLine(list2, roomLightEquipment.Room, point);
					}
					return roomLightEquipment;
				}
			}
			return null;
		}

		private List<Element> FilterWalls(List<Element> walls, List<Element> spaceWalls)
		{
			List<Element> list = new List<Element>();
			foreach (Element element in walls)
			{
				foreach (Element element2 in spaceWalls)
				{
					if (element2 != null && element.Id == element2.Id)
					{
						list = this.AddElement(list, new List<Element>
						{
							element
						});
					}
				}
			}
			return list;
		}

		private List<Element> JudgeLeftOrRight(List<Element> list, Element line, Room room)
		{
			LocationPoint locationPoint = room.Location as LocationPoint;
			XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
			LocationCurve locationCurve = list[0].Location as LocationCurve;
			XYZ project = new YJKLine(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1)).GetProject(xyz);
			LocationCurve locationCurve2 = line.Location as LocationCurve;
			XYZ project2 = new YJKLine(locationCurve2.Curve.GetEndPoint(0), locationCurve2.Curve.GetEndPoint(1)).GetProject(xyz);
			bool flag = this.JustPointBesideLineLorR(project, xyz, project2);
			List<Element> list2 = new List<Element>();
			if (flag)
			{
				list2.Add(list[0]);
				list2.Add(list[1]);
			}
			else
			{
				list2.Add(list[1]);
				list2.Add(list[0]);
			}
			return list2;
		}

		private bool JustPointBesideLineLorR(XYZ basePt, XYZ startPt, XYZ endPt)
		{
			YJKLine hyline = new YJKLine(startPt, endPt);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(basePt);
			XYZ xyz = (endPt - startPt).Normalize();
			xyz = new XYZ(xyz.X, xyz.Y, 0.0).Normalize();
			XYZ xyz2 = basePt - project;
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			return xyz.CrossProduct(xyz2.Normalize()).Normalize().IsAlmostEqualTo(XYZ.BasisZ);
		}

		private XYZ GetLayPointByLine(List<Element> list, Room room, XYZ point2)
		{
			XYZ xyz = null;
			if (list.Count == 2)
			{
				xyz = this.GetLayPoint(list[0], room, point2);
				if (xyz == null)
				{
					xyz = this.GetLayPoint(list[1], room, point2);
				}
			}
			else if (list.Count == 1)
			{
				xyz = this.GetLayPoint(list[0], room, point2);
			}
			return xyz;
		}

		private XYZ GetLayPoint(Element element, Room room, XYZ point1)
		{
			List<Curve> curves = this.listCurves[room];
			Curve wallCurve = this.GetWallCurve(curves, element, null);
			if (wallCurve == null)
			{
				return null;
			}
			List<Element> elementList = this.GetElementList(wallCurve.GetEndPoint(0), wallCurve.GetEndPoint(1), BuiltInCategory.OST_Doors);
			Element element2 = null;
			foreach (Element element3 in elementList)
			{
				FamilyInstance familyInstance = element3 as FamilyInstance;
				if (element.Id == familyInstance.Host.Id)
				{
					element2 = element3;
				}
			}
			XYZ xyz = new XYZ();
			if (element2 != null)
			{
				xyz = this.GetLayPoint3(point1, wallCurve, element2);
				if (xyz == null)
				{
					xyz = this.GetLayPoint4(point1, wallCurve, element2);
				}
			}
			else
			{
				xyz = this.GetLayPoint5(point1, wallCurve);
				if (xyz == null)
				{
					xyz = this.GetLayPoint6(point1, wallCurve);
				}
			}
			return xyz;
		}

		private List<Element> GetModelLine(List<Element> list)
		{
			List<Element> list2 = new List<Element>();
			foreach (Element element in list)
			{
				LocationCurve locationCurve = element.Location as LocationCurve;
				List<Element> elementList = this.GetElementList(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1), BuiltInCategory.OST_RoomSeparationLines);
				list2 = this.AddElement(list2, elementList);
			}
			if (list2.Count == list.Count)
			{
				return list2;
			}
			return this.GetModelLine(list2);
		}

		private List<Element> GetWallEquipment(List<Element> list)
		{
			List<Element> list2 = new List<Element>();
			foreach (Element element in list)
			{
				LocationCurve locationCurve = element.Location as LocationCurve;
				List<Element> wallList = this.GetWallList(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1));
				XYZ endPoint = locationCurve.Curve.GetEndPoint(0);
				XYZ endPoint2 = locationCurve.Curve.GetEndPoint(1);
				if (wallList.Count == 0)
				{
					return null;
				}
				Wall wall = wallList[0] as Wall;
				double halfSize = 0.0;
				if (wall != null)
				{
					halfSize = wall.WallType.GetParameter("厚度").AsDouble();
				}
				List<Element> elements = this.GetElements(endPoint, halfSize);
				list2 = this.AddElement(list2, elements);
				List<Element> elements2 = this.GetElements(endPoint2, halfSize);
				list2 = this.AddElement(list2, elements2);
			}
			return list2;
		}

		private List<Element> GetElements(XYZ searchPoint, double halfSize)
		{
			XYZ point = new XYZ(searchPoint.X - halfSize, searchPoint.Y - halfSize, searchPoint.Z - halfSize);
			XYZ point2 = new XYZ(searchPoint.X + halfSize, searchPoint.Y + halfSize, searchPoint.Z + halfSize);
			return this.GetWallList(point, point2);
		}

		private List<Element> GetElements(XYZ searchPoint, double halfSize, BuiltInCategory builtInCategory)
		{
			XYZ point = new XYZ(searchPoint.X - halfSize, searchPoint.Y - halfSize, searchPoint.Z - halfSize);
			XYZ point2 = new XYZ(searchPoint.X + halfSize, searchPoint.Y + halfSize, searchPoint.Z + halfSize);
			return this.GetElementList(point, point2, builtInCategory);
		}

		private RoomLightEquipmentWallInfo GetWallInfo(RoomLightEquipment roomLightEquipment, bool OutSideRoomLay = false)
		{
			RoomLightEquipmentWallInfo roomLightEquipmentWallInfo = new RoomLightEquipmentWallInfo();
			LocationPoint locationPoint = roomLightEquipment.Room.Location as LocationPoint;
			XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, locationPoint.Point.Z);
			LocationPoint locationPoint2 = roomLightEquipment.Doors[0].Location as LocationPoint;
			XYZ xyz2 = new XYZ(locationPoint2.Point.X, locationPoint2.Point.Y, locationPoint2.Point.Z);
			FamilyInstance familyInstance = roomLightEquipment.Doors[0] as FamilyInstance;
			double num = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			Element host = familyInstance.Host;
			List<Curve> curves = this.listCurves[roomLightEquipment.Room];
			if (OutSideRoomLay)
			{
				Curve wallCurve = this.GetWallCurve(curves, host, xyz2);
				Curve wallCurve2 = this.GetWallCurve(curves, host, null);
				if (wallCurve != wallCurve2)
				{
					XYZ xyz3 = new YJKLine(wallCurve2.GetEndPoint(0), wallCurve2.GetEndPoint(1)).GetProject(xyz) - xyz;
					XYZ xyz4 = new XYZ(xyz3.X, xyz3.Y, 0.0);
					roomLightEquipmentWallInfo.Curve = this.CurCurve(wallCurve2, wallCurve, xyz4.Normalize(), num);
				}
				else
				{
					roomLightEquipmentWallInfo.Curve = wallCurve;
				}
			}
			else
			{
				roomLightEquipmentWallInfo.Curve = this.GetWallCurve(curves, host, null);
			}
			if (roomLightEquipmentWallInfo.Curve == null)
			{
				return null;
			}
			XYZ endPoint = roomLightEquipmentWallInfo.Curve.GetEndPoint(0);
			XYZ endPoint2 = roomLightEquipmentWallInfo.Curve.GetEndPoint(1);
			LocationCurve locationCurve = host.Location as LocationCurve;
			YJKLine hyline = new YJKLine(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1));
			XYZ project = hyline.GetProject(endPoint);
			XYZ project2 = hyline.GetProject(endPoint2);
			XYZ xyz5 = project - xyz2;
			XYZ xyz6 = project2 - xyz2;
			if (this.JustPointBesideLineLorR(project, xyz, xyz2))
			{
				roomLightEquipmentWallInfo.LeftVector = xyz5;
				roomLightEquipmentWallInfo.RightVector = xyz6;
				roomLightEquipmentWallInfo.LeftLength = Math.Round((project.DistanceTo(xyz2) - num / 2.0) * 304.8, 1, MidpointRounding.AwayFromZero);
				roomLightEquipmentWallInfo.RightLength = Math.Round((project2.DistanceTo(xyz2) - num / 2.0) * 304.8, 1, MidpointRounding.AwayFromZero);
			}
			else
			{
				roomLightEquipmentWallInfo.LeftVector = xyz6;
				roomLightEquipmentWallInfo.RightVector = xyz5;
				roomLightEquipmentWallInfo.RightLength = Math.Round((project.DistanceTo(xyz2) - num / 2.0) * 304.8, 1, MidpointRounding.AwayFromZero);
				roomLightEquipmentWallInfo.LeftLength = Math.Round((project2.DistanceTo(xyz2) - num / 2.0) * 304.8, 1, MidpointRounding.AwayFromZero);
			}
			return roomLightEquipmentWallInfo;
		}

		private Curve CurCurve(Curve longCurve, Curve shortCurve, XYZ vector, double doorWidth)
		{
			XYZ xyz = new XYZ();
			XYZ xyz2 = new XYZ();
			if (shortCurve.GetEndPoint(0).IsAlmostEqualTo(longCurve.GetEndPoint(0), 0.001) || shortCurve.GetEndPoint(0).IsAlmostEqualTo(longCurve.GetEndPoint(1), 0.001))
			{
				xyz = shortCurve.GetEndPoint(0);
			}
			else
			{
				XYZ searchPoint = shortCurve.GetEndPoint(0) + vector * (doorWidth + 0.2);
				List<Element> elements = this.GetElements(searchPoint, 0.01);
				if (elements != null && elements.Count != 0)
				{
					Wall wall = elements[0] as Wall;
					if (wall != null)
					{
						double num = wall.WallType.GetParameter("厚度").AsDouble();
						xyz = shortCurve.GetEndPoint(0) + (shortCurve.GetEndPoint(1) - shortCurve.GetEndPoint(0)).Normalize() * num / 2.0;
					}
					else
					{
						xyz = shortCurve.GetEndPoint(0);
					}
				}
				else
				{
					xyz = shortCurve.GetEndPoint(0);
				}
			}
			if (shortCurve.GetEndPoint(1).IsAlmostEqualTo(longCurve.GetEndPoint(0), 0.001) || shortCurve.GetEndPoint(1).IsAlmostEqualTo(longCurve.GetEndPoint(1), 0.001))
			{
				xyz2 = shortCurve.GetEndPoint(1);
			}
			else
			{
				XYZ searchPoint2 = shortCurve.GetEndPoint(1) + vector * (doorWidth + 0.2);
				List<Element> elements2 = this.GetElements(searchPoint2, 0.01);
				if (elements2 != null && elements2.Count != 0)
				{
					Wall wall2 = elements2[0] as Wall;
					if (wall2 != null)
					{
						double num2 = wall2.WallType.GetParameter("厚度").AsDouble();
						xyz2 = shortCurve.GetEndPoint(1) + (shortCurve.GetEndPoint(0) - shortCurve.GetEndPoint(1)).Normalize() * num2 / 2.0;
					}
					else
					{
						xyz2 = shortCurve.GetEndPoint(1);
					}
				}
				else
				{
					xyz2 = shortCurve.GetEndPoint(1);
				}
			}
			return Line.CreateBound(xyz, xyz2);
		}

		private Curve GetWallCurve(List<Curve> curves, Element elem, XYZ point = null)
		{
			LocationCurve locationCurve = elem.Location as LocationCurve;
			YJKLine hyline = new YJKLine(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1));
			Wall wall = elem as Wall;
			double num = 0.0;
			if (wall != null)
			{
				num = wall.WallType.GetParameter("厚度").AsDouble() / 2.0;
			}
			List<Curve> list = new List<Curve>();
			foreach (Curve curve in curves)
			{
				XYZ endPoint = curve.GetEndPoint(0);
				XYZ endPoint2 = curve.GetEndPoint(1);
				YJKLine hyline2 = new YJKLine(endPoint, endPoint2);
				if (hyline.IsParallel(hyline2.Line))
				{
					XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
					XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
					XYZ endPoint3 = locationCurve.Curve.GetEndPoint(0);
					XYZ endPoint4 = locationCurve.Curve.GetEndPoint(1);
					XYZ start = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
					XYZ end = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
					YJKLine hyline3 = new YJKLine(start, end);
					hyline3.MakeUnBound();
					YJKLine hyline4 = new YJKLine(start, end);
					XYZ point2 = (xyz + xyz2) / 2.0;
					XYZ project = hyline3.GetProject(point2);
					bool flag = false;
					if (hyline4.IsPointOnLine(project))
					{
						flag = true;
					}
					if (Math.Abs(hyline3.GetDistance(xyz) - num) < 0.001 && Math.Abs(hyline3.GetDistance(xyz2) - num) < 0.001 && flag)
					{
						if (point != null)
						{
							YJKLine hyline5 = new YJKLine(xyz, xyz2);
							YJKLine hyline6 = new YJKLine(xyz, xyz2);
							hyline5.MakeUnBound();
							XYZ project2 = hyline5.GetProject(point);
							if (hyline6.IsPointOnLine(project2))
							{
								return curve;
							}
						}
						list.Add(curve);
					}
				}
			}
			if (list == null || list.Count == 0)
			{
				return null;
			}
			if (list.Count == 1)
			{
				return list[0];
			}
			bool flag2 = false;
			List<Curve> lstTemp = CmdRoomLightSetting.SortListCurve(list, out flag2);
			return this.MergeCurve(lstTemp);
		}

		public static List<Curve> SortListCurve(List<Curve> lstLine, out bool bl)
		{
			List<Curve> list = new List<Curve>();
			List<XYZ> list2 = new List<XYZ>();
			foreach (Curve curve in lstLine)
			{
				list2.Add(curve.GetEndPoint(0));
				list2.Add(curve.GetEndPoint(1));
			}
			XYZ leftTopPoint = CmdRoomLightSetting.GetLeftTopPoint(list2);
			Curve curve2 = lstLine[0];
			foreach (Curve curve3 in lstLine)
			{
				if (curve3.GetEndPoint(0).DistanceTo(leftTopPoint) < 0.001)
				{
					curve2 = curve3;
					break;
				}
			}
			Curve curve4 = curve2;
			foreach (Curve curve5 in lstLine)
			{
				foreach (Curve curve6 in lstLine)
				{
					if (!list.Contains(curve6))
					{
						if (curve4.GetEndPoint(1).DistanceTo(curve6.GetEndPoint(0)) < 0.1 || curve4.GetEndPoint(1).DistanceTo(curve6.GetEndPoint(1)) < 0.1)
						{
							list.Add(curve6);
							curve4 = curve6;
							break;
						}
						if (curve4.GetEndPoint(0).DistanceTo(curve6.GetEndPoint(0)) < 0.1 || curve4.GetEndPoint(0).DistanceTo(curve6.GetEndPoint(1)) < 0.1)
						{
							list.Add(curve6);
							curve4 = curve6;
							break;
						}
					}
				}
				if (list.Count == lstLine.Count)
				{
					break;
				}
			}
			if (list.Count != lstLine.Count)
			{
				bl = true;
			}
			else
			{
				bl = false;
			}
			return list;
		}

		public static XYZ GetLeftTopPoint(List<XYZ> lstPoint)
		{
			XYZ xyz = lstPoint[0];
			foreach (XYZ xyz2 in lstPoint)
			{
				if (xyz2.Y > xyz.Y)
				{
					xyz = xyz2;
				}
			}
			foreach (XYZ xyz3 in lstPoint)
			{
				if (Math.Round(xyz3.Y, 2) == Math.Round(xyz.Y, 2) && xyz3.X < xyz.X)
				{
					xyz = xyz3;
				}
			}
			return xyz;
		}

		private Curve MergeCurve(List<Curve> lstTemp)
		{
			List<Curve> list = new List<Curve>();
			list.Add(lstTemp[0]);
			foreach (Curve curve in lstTemp)
			{
				if (list.Last<Curve>().GetEndPoint(1).DistanceTo(curve.GetEndPoint(0)) < 0.1)
				{
					Curve item = Line.CreateBound(list.Last<Curve>().GetEndPoint(0), curve.GetEndPoint(1));
					list.Remove(list.Last<Curve>());
					list.Add(item);
					break;
				}
			}
			return list[0];
		}

		private XYZ GetVector(Curve curve1, Curve curve2, ref XYZ interPoint)
		{
			XYZ endPoint = curve1.GetEndPoint(0);
			XYZ endPoint2 = curve1.GetEndPoint(1);
			XYZ start = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ end = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			XYZ endPoint3 = curve2.GetEndPoint(0);
			XYZ endPoint4 = curve2.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
			YJKLine hyline = new YJKLine(start, end);
			YJKLine hyline2 = new YJKLine(xyz, xyz2);
			hyline.MakeUnBound();
			hyline2.MakeUnBound();
			interPoint = hyline.IntersectionPoint(hyline2.Line);
			new XYZ();
			XYZ result = new XYZ();
			if (interPoint.IsAlmostEqualTo(xyz, 0.0001))
			{
				result = interPoint - xyz2;
			}
			else
			{
				result = interPoint - xyz;
			}
			return result;
		}

		private XYZ GetLayPoint1(Curve curve1, Curve curve2)
		{
			XYZ endPoint = curve1.GetEndPoint(0);
			XYZ endPoint2 = curve1.GetEndPoint(1);
			XYZ start = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ end = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			XYZ endPoint3 = curve2.GetEndPoint(0);
			XYZ endPoint4 = curve2.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
			YJKLine hyline = new YJKLine(start, end);
			YJKLine hyline2 = new YJKLine(xyz, xyz2);
			hyline.MakeUnBound();
			hyline2.MakeUnBound();
			XYZ xyz3 = hyline.IntersectionPoint(hyline2.Line);
			if (xyz3 == null)
			{
				return null;
			}
			if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
			{
				XYZ xyz4 = xyz2 - xyz3;
				return xyz3 + xyz4.Normalize() * 200.0 / 304.8;
			}
			XYZ xyz5 = xyz - xyz3;
			return xyz3 + xyz5.Normalize() * 200.0 / 304.8;
		}

		private XYZ GetLayPoint2(Curve curve1, Curve curve2, Element elem)
		{
			XYZ endPoint = curve1.GetEndPoint(0);
			XYZ endPoint2 = curve1.GetEndPoint(1);
			XYZ start = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ end = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			XYZ endPoint3 = curve2.GetEndPoint(0);
			XYZ endPoint4 = curve2.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
			YJKLine hyline = new YJKLine(start, end);
			YJKLine hyline2 = new YJKLine(xyz, xyz2);
			hyline.MakeUnBound();
			hyline2.MakeUnBound();
			XYZ xyz3 = hyline.IntersectionPoint(hyline2.Line);
			if (xyz3 == null)
			{
				return null;
			}
			double num = (elem as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			LocationPoint locationPoint = elem.Location as LocationPoint;
			XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			XYZ project = hyline2.GetProject(point);
			if (xyz3.DistanceTo(project) - num / 2.0 > 0.98425196850393692)
			{
				if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
				{
					XYZ xyz4 = xyz2 - xyz3;
					return xyz3 + xyz4.Normalize() * 200.0 / 304.8;
				}
				XYZ xyz5 = xyz - xyz3;
				return xyz3 + xyz5.Normalize() * 200.0 / 304.8;
			}
			else
			{
				if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
				{
					XYZ xyz6 = xyz2 - xyz3;
					return xyz3 + xyz6.Normalize() * (0.65616797900262469 + num / 2.0 + xyz3.DistanceTo(project));
				}
				XYZ xyz7 = xyz - xyz3;
				return xyz3 + xyz7.Normalize() * (0.65616797900262469 + num / 2.0 + xyz3.DistanceTo(project));
			}
		}

		private XYZ GetOutLayPoint1(Curve curve1, Curve curve2, double thickness, double thickness2, RoomLightEquipment roomLightEquipment)
		{
			XYZ endPoint = curve1.GetEndPoint(0);
			XYZ endPoint2 = curve1.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			XYZ endPoint3 = curve2.GetEndPoint(0);
			XYZ endPoint4 = curve2.GetEndPoint(1);
			XYZ xyz3 = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
			XYZ xyz4 = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
			YJKLine hyline = new YJKLine(xyz, xyz2);
			YJKLine hyline2 = new YJKLine(xyz3, xyz4);
			hyline.MakeUnBound();
			hyline2.MakeUnBound();
			XYZ xyz5 = hyline.IntersectionPoint(hyline2.Line);
			if (xyz5 == null)
			{
				return null;
			}
			XYZ xyz6 = new XYZ();
			XYZ xyz7 = new XYZ();
			if (xyz5.IsAlmostEqualTo(xyz3, 0.0001))
			{
				xyz7 = xyz5 - xyz4;
				xyz6 = xyz5 + xyz7.Normalize() * (200.0 + thickness) / 304.8;
			}
			else
			{
				xyz7 = xyz5 - xyz3;
				xyz6 = xyz5 + xyz7.Normalize() * (200.0 + thickness) / 304.8;
			}
			XYZ xyz8 = new XYZ();
			if (xyz5.IsAlmostEqualTo(xyz, 0.0001))
			{
				xyz8 = (xyz5 - xyz2).Normalize();
			}
			else
			{
				xyz8 = (xyz5 - xyz).Normalize();
			}
			XYZ xyz9 = xyz6 + xyz8 * thickness2 / 609.6;
			List<Element> elements = this.GetElements(xyz9, 0.01);
			if (elements == null || elements.Count == 0)
			{
				return null;
			}
			Curve curve3 = (elements[0].Location as LocationCurve).Curve;
			YJKLine hyline3 = new YJKLine(curve3.GetEndPoint(0), curve3.GetEndPoint(1));
			hyline3.MakeUnBound();
			if (this.GetElementList(curve3.GetEndPoint(0), curve3.GetEndPoint(1), BuiltInCategory.OST_Doors).Count == 0)
			{
				roomLightEquipment.Wall = (elements[0] as Wall);
				return xyz6;
			}
			XYZ project = hyline3.GetProject(xyz9);
			List<Element> elements2 = this.GetElements(project, 0.65616797900262469, (Autodesk.Revit.DB.BuiltInCategory)(-2000023));
			if (elements2.Count == 0)
			{
				roomLightEquipment.Wall = (elements[0] as Wall);
				return xyz6;
			}
			Element element = elements2[0];
			LocationPoint locationPoint = element.Location as LocationPoint;
			XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			XYZ project2 = hyline3.GetProject(point);
			XYZ project3 = hyline3.GetProject(xyz5);
			double num = (element as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			if (project.DistanceTo(project2) < 0.65616797900262469 + num / 2.0)
			{
				xyz6 = xyz5 + xyz7.Normalize() * (0.65616797900262469 + num / 2.0 + project3.DistanceTo(project2));
				XYZ searchPoint = xyz6 + xyz8 * thickness2 / 609.6;
				List<Element> elements3 = this.GetElements(searchPoint, 0.01);
				List<Element> elements4 = this.GetElements(searchPoint, 0.01, (Autodesk.Revit.DB.BuiltInCategory)(-2000023));
				if (elements3 != null && elements3.Count == 1 && elements4.Count == 0 && elements3[0].Id == elements[0].Id)
				{
					roomLightEquipment.Wall = (elements[0] as Wall);
					return xyz6;
				}
				return null;
			}
			else
			{
				XYZ searchPoint2 = xyz6 + xyz8 * thickness2 / 609.6;
				List<Element> elements5 = this.GetElements(searchPoint2, 0.01);
				List<Element> elements6 = this.GetElements(searchPoint2, 0.01, (Autodesk.Revit.DB.BuiltInCategory)(-2000023));
				if (elements5 != null && elements5.Count == 1 && elements6.Count == 0 && elements5[0].Id == elements[0].Id)
				{
					roomLightEquipment.Wall = (elements[0] as Wall);
					return xyz6;
				}
				return null;
			}
		}

		private XYZ GetOutLayPoint2(Curve curve1, Curve curve2, Element elem, double thickness, double thickness2, RoomLightEquipment roomLightEquipment)
		{
			XYZ endPoint = curve1.GetEndPoint(0);
			XYZ endPoint2 = curve1.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			XYZ endPoint3 = curve2.GetEndPoint(0);
			XYZ endPoint4 = curve2.GetEndPoint(1);
			XYZ xyz3 = new XYZ(endPoint3.X, endPoint3.Y, 0.0);
			XYZ xyz4 = new XYZ(endPoint4.X, endPoint4.Y, 0.0);
			YJKLine hyline = new YJKLine(xyz, xyz2);
			YJKLine hyline2 = new YJKLine(xyz3, xyz4);
			hyline.MakeUnBound();
			hyline2.MakeUnBound();
			XYZ xyz5 = hyline.IntersectionPoint(hyline2.Line);
			if (xyz5 == null)
			{
				return null;
			}
			double num = (elem as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			LocationPoint locationPoint = elem.Location as LocationPoint;
			XYZ point = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			XYZ project = hyline2.GetProject(point);
			XYZ xyz6 = new XYZ();
			if (xyz5.DistanceTo(project) - num / 2.0 > (300.0 + thickness) / 304.8)
			{
				if (xyz5.IsAlmostEqualTo(xyz3, 0.0001))
				{
					XYZ xyz7 = xyz5 - xyz4;
					xyz6 = xyz5 + xyz7.Normalize() * (200.0 + thickness) / 304.8;
				}
				else
				{
					XYZ xyz8 = xyz5 - xyz3;
					xyz6 = xyz5 + xyz8.Normalize() * (200.0 + thickness) / 304.8;
				}
			}
			else if (xyz5.IsAlmostEqualTo(xyz3, 0.0001))
			{
				XYZ xyz9 = xyz5 - xyz4;
				xyz6 = xyz5 + xyz9.Normalize() * (0.65616797900262469 + num / 2.0 + xyz5.DistanceTo(project));
			}
			else
			{
				XYZ xyz10 = xyz5 - xyz3;
				xyz6 = xyz5 + xyz10.Normalize() * (0.65616797900262469 + num / 2.0 + xyz5.DistanceTo(project));
			}
			XYZ xyz11 = new XYZ();
			if (xyz5.IsAlmostEqualTo(xyz, 0.0001))
			{
				xyz11 = (xyz5 - xyz2).Normalize();
			}
			else
			{
				xyz11 = (xyz5 - xyz).Normalize();
			}
			XYZ searchPoint = xyz6 + xyz11 * thickness2 / 609.6;
			List<Element> elements = this.GetElements(searchPoint, 0.01);
			if (elements != null && elements.Count != 0)
			{
				roomLightEquipment.Wall = (elements[0] as Wall);
				return xyz6;
			}
			return null;
		}

		private XYZ GetLayPoint3(XYZ point1, Curve curve2, Element elem)
		{
			XYZ endPoint = curve2.GetEndPoint(0);
			XYZ endPoint2 = curve2.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			double num = point1.DistanceTo(xyz);
			double num2 = point1.DistanceTo(xyz2);
			XYZ xyz3 = (num < num2) ? xyz : xyz2;
			double num3 = (elem as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			LocationPoint locationPoint = elem.Location as LocationPoint;
			XYZ xyz4 = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			if (xyz3.DistanceTo(xyz4) - num3 / 2.0 > 0.98425196850393692)
			{
				if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
				{
					XYZ xyz5 = xyz2 - xyz3;
					return xyz3 + xyz5.Normalize() * 200.0 / 304.8;
				}
				XYZ xyz6 = xyz - xyz3;
				return xyz3 + xyz6.Normalize() * 200.0 / 304.8;
			}
			else
			{
				if (xyz3.DistanceTo(xyz4) - num3 / 2.0 < 0.65616797900262469)
				{
					return null;
				}
				if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
				{
					XYZ xyz7 = xyz2 - xyz3;
					return xyz3 + xyz7.Normalize() * (xyz3.DistanceTo(xyz4) - num3 / 2.0) / 2.0;
				}
				XYZ xyz8 = xyz - xyz3;
				return xyz3 + xyz8.Normalize() * (xyz3.DistanceTo(xyz4) - num3 / 2.0) / 2.0;
			}
		}

		private XYZ GetLayPoint4(XYZ point1, Curve curve2, Element elem)
		{
			XYZ endPoint = curve2.GetEndPoint(0);
			XYZ endPoint2 = curve2.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			double num = point1.DistanceTo(xyz);
			double num2 = point1.DistanceTo(xyz2);
			XYZ xyz3 = (num < num2) ? xyz : xyz2;
			(elem as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			LocationPoint locationPoint = elem.Location as LocationPoint;
			new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
			{
				XYZ xyz4 = xyz2 - xyz3;
				return xyz3 + xyz4.Normalize() * 200.0 / 304.8;
			}
			XYZ xyz5 = xyz - xyz3;
			return xyz3 + xyz5.Normalize() * 200.0 / 304.8;
		}

		private XYZ GetLayPoint5(XYZ point1, Curve curve2)
		{
			XYZ endPoint = curve2.GetEndPoint(0);
			XYZ endPoint2 = curve2.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			double num = point1.DistanceTo(xyz);
			double num2 = point1.DistanceTo(xyz2);
			XYZ xyz3 = (num < num2) ? xyz : xyz2;
			if (xyz.DistanceTo(xyz2) > 0.98425196850393692)
			{
				if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
				{
					XYZ xyz4 = xyz2 - xyz3;
					return xyz3 + xyz4.Normalize() * 200.0 / 304.8;
				}
				XYZ xyz5 = xyz - xyz3;
				return xyz3 + xyz5.Normalize() * 200.0 / 304.8;
			}
			else
			{
				if (xyz.DistanceTo(xyz2) < 0.65616797900262469)
				{
					return null;
				}
				if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
				{
					XYZ xyz6 = xyz2 - xyz3;
					return xyz3 + xyz6.Normalize() * xyz.DistanceTo(xyz2) / 2.0;
				}
				XYZ xyz7 = xyz - xyz3;
				return xyz3 + xyz7.Normalize() * xyz.DistanceTo(xyz2) / 2.0;
			}
		}

		private XYZ GetLayPoint6(XYZ point1, Curve curve2)
		{
			XYZ endPoint = curve2.GetEndPoint(0);
			XYZ endPoint2 = curve2.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			double num = point1.DistanceTo(xyz);
			double num2 = point1.DistanceTo(xyz2);
			XYZ xyz3 = (num < num2) ? xyz : xyz2;
			if (xyz3.IsAlmostEqualTo(xyz, 0.0001))
			{
				XYZ xyz4 = xyz2 - xyz3;
				return xyz3 + xyz4.Normalize() * 200.0 / 304.8;
			}
			XYZ xyz5 = xyz - xyz3;
			return xyz3 + xyz5.Normalize() * 200.0 / 304.8;
		}

		private List<Element> GetWallList(XYZ point1, XYZ point2)
		{
			List<Element> list = new List<Element>();
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(RevitAPIUIOperate.CreatOutLine(RevitAPIUIOperate.GetViewRange2(base.Doc.ActiveView, false), point1, point2));
			foreach (Element item in new FilteredElementCollector(base.Doc).OfClass(typeof(Wall)).WherePasses(boundingBoxIntersectsFilter).ToElements())
			{
				list.Add(item);
			}
			IEnumerable<Document> linkDocuments = base.Doc.GetLinkDocuments();
			if (linkDocuments == null)
			{
				return list;
			}
			foreach (Autodesk.Revit.DB.Document document in linkDocuments)
			{
				if (document != null)
				{
					List<Element> wallFormDoc = this.GetWallFormDoc(document, boundingBoxIntersectsFilter);
					list.AddRange(wallFormDoc);
				}
			}
			return list;
		}

		private List<Element> GetWallFormDoc(Autodesk.Revit.DB.Document doc, BoundingBoxIntersectsFilter filter)
		{
			new List<Element>();
			new List<Element>();
			return new FilteredElementCollector(doc).OfClass(typeof(Wall)).WherePasses(filter).ToElements().ToList<Element>();
		}

		private List<Element> GetElementList(XYZ point1, XYZ point2, BuiltInCategory category)
		{
			List<Element> list = new List<Element>();
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(RevitAPIUIOperate.CreatOutLine(RevitAPIUIOperate.GetViewRange2(base.Doc.ActiveView, false), point1, point2));
			foreach (Element item in new FilteredElementCollector(base.Doc).OfCategory(category).WherePasses(boundingBoxIntersectsFilter).ToElements())
			{
				list.Add(item);
			}
			IEnumerable<Document> linkDocuments = base.Doc.GetLinkDocuments();
			if (linkDocuments == null)
			{
				return list;
			}
			foreach (Autodesk.Revit.DB.Document document in linkDocuments)
			{
				if (document != null)
				{
					List<Element> elementFormDoc = this.GetElementFormDoc(document, boundingBoxIntersectsFilter, category);
					list.AddRange(elementFormDoc);
				}
			}
			return list;
		}

		private List<Element> GetElementFormDoc(Autodesk.Revit.DB.Document doc, BoundingBoxIntersectsFilter filter, BuiltInCategory category)
		{
			new List<Element>();
			new List<Element>();
			return new FilteredElementCollector(doc).OfCategory(category).WherePasses(filter).ToElements().ToList<Element>();
		}

		private FamilySymbol GetFamilySymbol(int ruleLay, List<DbFamilyObject> familyObjects, ref DbFamilyObject familyObject)
		{
			FamilySymbol result = null;
			string text = "";
			if (ruleLay == 2)
			{
				result = YJKRevitTools.GetFamilySymbolByName("Lamp", "密闭环形管吸顶灯", base.Doc, ref text);
				using (List<DbFamilyObject>.Enumerator enumerator = familyObjects.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DbFamilyObject dbFamilyObject = enumerator.Current;
						if (dbFamilyObject.Name == "密闭环形管吸顶灯")
						{
							familyObject = dbFamilyObject;
						}
					}
					return result;
				}
			}
			result = YJKRevitTools.GetFamilySymbolByName("Lamp", "环形管吸顶灯", base.Doc, ref text);
			foreach (DbFamilyObject dbFamilyObject2 in familyObjects)
			{
				if (dbFamilyObject2.Name == "环形管吸顶灯")
				{
					familyObject = dbFamilyObject2;
				}
			}
			return result;
		}

		private FamilySymbol GetFamilySymbol2(int ruleLay, List<DbFamilyObject> familyObjects, ref DbFamilyObject familyObject)
		{
			FamilySymbol result = null;
			string text = "";
			if (ruleLay == 1)
			{
				result = YJKRevitTools.GetFamilySymbolByName("Switch", "单联单控开关", base.Doc, ref text);
				using (List<DbFamilyObject>.Enumerator enumerator = familyObjects.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DbFamilyObject dbFamilyObject = enumerator.Current;
						if (dbFamilyObject.Name == "单联单控开关")
						{
							familyObject = dbFamilyObject;
						}
					}
					return result;
				}
			}
			if (ruleLay == 2)
			{
				result = YJKRevitTools.GetFamilySymbolByName("Switch", "双联单控开关", base.Doc, ref text);
				using (List<DbFamilyObject>.Enumerator enumerator = familyObjects.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DbFamilyObject dbFamilyObject2 = enumerator.Current;
						if (dbFamilyObject2.Name == "双联单控开关")
						{
							familyObject = dbFamilyObject2;
						}
					}
					return result;
				}
			}
			if (ruleLay == 3)
			{
				result = YJKRevitTools.GetFamilySymbolByName("Switch", "三联单控开关", base.Doc, ref text);
				using (List<DbFamilyObject>.Enumerator enumerator = familyObjects.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DbFamilyObject dbFamilyObject3 = enumerator.Current;
						if (dbFamilyObject3.Name == "三联单控开关")
						{
							familyObject = dbFamilyObject3;
						}
					}
					return result;
				}
			}
			result = YJKRevitTools.GetFamilySymbolByName("Switch", "四联单控开关", base.Doc, ref text);
			foreach (DbFamilyObject dbFamilyObject4 in familyObjects)
			{
				if (dbFamilyObject4.Name == "四联单控开关")
				{
					familyObject = dbFamilyObject4;
				}
			}
			return result;
		}

		private List<string> GetRoomName(int type)
		{
			XmlDocument xmlDoc = XMLOperating.LoadXml(Path.Combine(Product.DataLocation, "ElectricalData", "KewWordSet.xml"));
			List<string> list = new List<string>();
			foreach (object obj in XMLOperating.GetXmlNodeByAttribute(xmlDoc, "KewWordSet", "type", type.ToString()).ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				list.Add((xmlNode as XmlElement).InnerText);
			}
			return list;
		}

		private Dictionary<List<string>, List<string>> GetAscriptionInfo(int type)
		{
			Dictionary<List<string>, List<string>> dictionary = new Dictionary<List<string>, List<string>>();
			XmlDocument xmlDoc = XMLOperating.LoadXml(Path.Combine(Product.DataLocation, "ElectricalData", "KewWordSet.xml"));
			new List<string>();
			foreach (object obj in XMLOperating.GetXmlNodeByAttribute(xmlDoc, "KewWordSet", "type", type.ToString()).ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				List<string> key = (xmlNode as XmlElement).GetAttribute("key").Split(new char[]
				{
					','
				}).ToList<string>();
				List<string> value = (xmlNode as XmlElement).GetAttribute("value").Split(new char[]
				{
					','
				}).ToList<string>();
				dictionary.Add(key, value);
			}
			return dictionary;
		}

		private List<Room> RoomFilter(List<Element> list)
		{
			List<Room> list2 = new List<Room>();
			foreach (Element element in list)
			{
				if (element is Room)
				{
					list2.Add(element as Room);
				}
			}
			return list2;
		}

		private List<Room> RoomFilter2(List<Element> list, List<Room> rooms)
		{
			List<ElementId> list2 = new List<ElementId>();
			foreach (Room room in rooms)
			{
				list2.Add(room.Id);
			}
			List<Room> list3 = new List<Room>();
			foreach (Element element in list)
			{
				if (element is Room && list2.Contains(element.Id))
				{
					list3.Add(element as Room);
				}
			}
			return list3;
		}

		private List<RoomLightEquipment> GetRoomLightEquipmentInfo(List<Room> rooms)
		{
			List<DoorRoomCorresponding> doorRoomCorresponding = this.GetDoorRoomCorresponding(rooms);
			List<DoorRoomCorresponding> listDoorRoomCorresponding = this.JudgeDoorsAscription(doorRoomCorresponding);
			return this.StorageRoomLightEquipment(listDoorRoomCorresponding);
		}

		private List<RoomLightEquipment> FilterRoomLightEquipmentOfRoom(List<Room> rooms, List<RoomLightEquipment> roomLightEquipments)
		{
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			foreach (RoomLightEquipment roomLightEquipment in roomLightEquipments)
			{
				bool flag = false;
				using (List<Room>.Enumerator enumerator2 = rooms.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Id == roomLightEquipment.Room.Id)
						{
							flag = true;
							break;
						}
					}
				}
				if (!flag)
				{
					list.Add(roomLightEquipment);
				}
			}
			foreach (RoomLightEquipment item in list)
			{
				roomLightEquipments.Remove(item);
			}
			foreach (Room room in rooms)
			{
				bool flag2 = false;
				using (List<RoomLightEquipment>.Enumerator enumerator = roomLightEquipments.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						if (enumerator.Current.Room.Id == room.Id)
						{
							flag2 = true;
						}
					}
				}
				if (!flag2)
				{
					roomLightEquipments.Add(new RoomLightEquipment
					{
						Room = room
					});
				}
			}
			return roomLightEquipments;
		}

		private List<DoorRoomCorresponding> GetDoorRoomCorresponding(List<Room> rooms)
		{
			List<DoorRoomCorresponding> list = new List<DoorRoomCorresponding>();
			foreach (Room room in rooms)
			{
				foreach (Element element in YJKRevitTools.GetDoorsOrWindowsInSpaceOrRoom(room, BuiltInCategory.OST_Doors))
				{
					bool flag = false;
					foreach (DoorRoomCorresponding doorRoomCorresponding in list)
					{
						if (doorRoomCorresponding.Door.Id == element.Id && !doorRoomCorresponding.Room.Contains(room))
						{
							flag = true;
							doorRoomCorresponding.Room.Add(room);
							break;
						}
					}
					if (!flag)
					{
						list.Add(new DoorRoomCorresponding
						{
							Door = element,
							Room = 
							{
								room
							}
						});
					}
				}
			}
			return list;
		}

		private List<DoorRoomCorresponding> JudgeDoorsAscription(List<DoorRoomCorresponding> listDoorRoomCorresponding)
		{
			Dictionary<List<string>, List<string>> ascriptionInfo = this.GetAscriptionInfo(5);
			foreach (DoorRoomCorresponding doorRoomCorresponding in listDoorRoomCorresponding)
			{
				bool flag = false;
				bool flag2 = false;
				Room item = null;
				if (doorRoomCorresponding.Room.Count == 2 && doorRoomCorresponding.Room.First<Room>().GetParameter("名称") != null && doorRoomCorresponding.Room.Last<Room>().GetParameter("名称") != null)
				{
					string text = doorRoomCorresponding.Room.First<Room>().GetParameter("名称").AsString();
					string text2 = doorRoomCorresponding.Room.Last<Room>().GetParameter("名称").AsString();
					foreach (KeyValuePair<List<string>, List<string>> keyValuePair in ascriptionInfo)
					{
						foreach (string value in keyValuePair.Key)
						{
							if (text.Contains(value))
							{
								flag = true;
								break;
							}
							if (text2.Contains(value))
							{
								flag2 = true;
								break;
							}
						}
						foreach (string value2 in keyValuePair.Value)
						{
							if (text.Contains(value2))
							{
								flag = true;
								item = doorRoomCorresponding.Room.First<Room>();
								break;
							}
							if (text2.Contains(value2))
							{
								flag2 = true;
								item = doorRoomCorresponding.Room.Last<Room>();
								break;
							}
						}
						if (flag && flag2)
						{
							break;
						}
						flag = false;
						flag2 = false;
						item = null;
					}
					if (flag && flag2)
					{
						doorRoomCorresponding.Room.Remove(item);
					}
					else
					{
						FamilyInstance familyInstance = doorRoomCorresponding.Door as FamilyInstance;
						if (familyInstance.Symbol.FamilyName.Contains("推拉门"))
						{
							doorRoomCorresponding.Room.Remove(familyInstance.FromRoom);
						}
						else
						{
							doorRoomCorresponding.Room.Remove(this.FilterRoomAscription(doorRoomCorresponding));
						}
					}
				}
			}
			return listDoorRoomCorresponding;
		}

		private Room FilterRoomAscription(DoorRoomCorresponding item)
		{
			FamilyInstance familyInstance = item.Door as FamilyInstance;
			LocationPoint locationPoint = familyInstance.Location as LocationPoint;
			XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 0.0);
			XYZ facingOrientation = familyInstance.FacingOrientation;
			XYZ pt = xyz + facingOrientation;
			List<Curve> lstCurves = this.listCurves[item.Room.First<Room>()];
			if (this.PointInPloygon(lstCurves, pt) == 1)
			{
				return item.Room.Last<Room>();
			}
			return item.Room.First<Room>();
		}

		private int PointInPloygon(List<Curve> lstCurves, XYZ pt)
		{
			return XYZUtilityMethod.PointInPloygon(lstCurves, pt);
		}

		private List<RoomLightEquipment> StorageRoomLightEquipment(List<DoorRoomCorresponding> listDoorRoomCorresponding)
		{
			List<RoomLightEquipment> list = new List<RoomLightEquipment>();
			foreach (DoorRoomCorresponding doorRoomCorresponding in listDoorRoomCorresponding)
			{
				if (doorRoomCorresponding.Room != null && doorRoomCorresponding.Room.Count != 0)
				{
					bool flag = false;
					foreach (RoomLightEquipment roomLightEquipment in list)
					{
						if (roomLightEquipment.Room.Id == doorRoomCorresponding.Room[0].Id && !roomLightEquipment.Doors.Contains(doorRoomCorresponding.Door))
						{
							flag = true;
							roomLightEquipment.Doors.Add(doorRoomCorresponding.Door);
							break;
						}
					}
					if (!flag)
					{
						list.Add(new RoomLightEquipment
						{
							Room = doorRoomCorresponding.Room[0],
							Doors = 
							{
								doorRoomCorresponding.Door
							}
						});
					}
				}
			}
			return list;
		}

		public static YJKWindowHandle hWndRevit;

		public Dictionary<Room, List<Curve>> listCurves = new Dictionary<Room, List<Curve>>();

		public List<List<Element>> listConnectWires = new List<List<Element>>();

		public Dictionary<Room, List<Element>> listFilterRooms = new Dictionary<Room, List<Element>>();
	}
}
